常规封装 (将具体初始化放在构造函数里面)
function Fruit (name){
this.name = name;
}
Fruit.prototype = {
constructor: Fruit, //将Fruit.prototype的构造函数指向Fruit
consoleName:function(){
console.log(this.name)
}
}
常规封装(放一个初始化方法在构造函数里面)
function Fruit (info){
this.init(info);
}
Fruit.prototype = {
constructor: Fruit, //将Fruit.prototype的构造函数指向Fruit
init: function(info){
this.name = info.name;
},
consoleName:function(){
console.log(this.name)
}
}
类jQuery 封装
var Fruit = function (info){
return new Fruit.prototype.init(info);
}
Fruit.prototype = {
constructor: Fruit,
init:function(info){
this.name = info.name;
}
}
Fruit.prototype.init.prorotype = Fruit.prototype; //将init的原型对象指定为Fruit的原型对象
var Fruit = function (info){
return new Fruit.fn.init(info);
}
Fruit.fn = Fruit.prototype = {
constructor: Fruit,
init:function(info){
this.name = info.name;
}
}
Fruit.fn.init.prorotype = Fruit.fn;
var Fruit = (function(window){
var Fruit = function (info){
return new Fruit.fn.init(info);
}
Fruit.fn = Fruit.prototype = {
constructor: Fruit,
init:function(info){
this.name = info.name;
}
}
Fruit.fn.init.prorotype = Fruit.fn;
return Fruit;
})()
原文链接:http://www.cnblogs.com/likeFlyingFish/p/6175734.html