网络技术 > 前端设计
javascript的类的封装
分类:前端设计    时间:2019-09-28    浏览:2588次
//1.类的封装
var shao = function(options){
this.id = options.id || 1;
this.title = options.title || "这是标题";
this.Init();
}
shao.prototype = {
Init:function(){
var str = "id:"+this.id+" title:"+this.title;
return str;
},
show:function(){
var str = "id:"+this.id+" title:"+this.title;
console.log(str);
}
}
//new shao({id:"2", title:"哈哈哈"}).Init();


//2.json模式封装
var shaooo = {
ver : "EditPage",
Init : function(type,tag){
EditPage.set(tag);
if(type==1){
EditPage.show();
}
else{
EditPage.edit();
}
},
set : function(tag){
if(tag!=""){
this.tag = tag;
}
else{
this.tag = "body";
}

},
show : function(){
$(this.tag).attr("contentEditable","true");
},
edit : function(){
var o = window.location.href;
if(o.indexOf("#edit")>0){
$(this.tag).attr("contentEditable","true");
}
}
};
//shaooo.Init();


//3.闭包封装
var shaonet = shaonet || {};
(function (n){
var ver = {"author":"shaonet","dt":"2018-06-29","ver":"1.01"};
var version = {
url:"https://www.xfengnet.com/",
Init:function(){
this.info();
return this.url;
},
info:function(){
console.log(this.url);
console.log(ver);
console.log("%c","color:red");
$.each(ver,function(i,v){
console.log(i+":"+ver[i]);
});
console.log("%c","color:red");
}
};
n.Init = function(){
var ver = version.Init();
console.log(ver);
$("#btn_do1").click(function(){
var id = $("#txt1").val();
n.getA(id);
});
};
//aspJSON A
n.getA = function(id){
var d = {};
if(id>=1){
d.id = id;
}else{
d.id = 1;
};
$.post("data.asp?act=geta", d, function(t){
console.log(t);
var str = "ID:"+t.ID+"";
str += "uName:"+t.uName+"";
str += "Company:"+t.Company+"";
str += "email:"+t.email+"";
str += "LastUpdate:"+t.LastUpdate;
$(".tag1").html(str);
},"json");
};
})(shaonet);
//shaonet.Init();
Copyright © 2014-2020 XfengNet.COM Inc. All Rights Reserved. 苏ICP备08014032号
返回顶部