var aQuery = function(selector, context) {
return aQuery.prototype.init();
}
aQuery.prototype = {
init:function(){
return this;
},
name:function(){console.log('name')},
age:function(){console.log('age')}
}
var it = aQuery()
console.log(it == aQuery.prototype)
it.name();
//this指向的是aQuery.prototype。
var aQuery2 = function(selector, context) {
return aQuery2.prototype.init();
}
aQuery2.prototype = {
init: function() {
this.age = 18
return this;
},
name: function() {},
age: 20
}
console.log(aQuery2().age) //18
//this指向的是aQuery.prototype。所以不是实例化。
var aQuery3 = function(selector, context) {
return new aQuery3.prototype.init();
}
aQuery3.prototype = {
init: function() {
this.age = 18
return this;
},
name: function() {},
age: 20
}
//Uncaught TypeError: Object [object Object] has no method 'name'
//this指向new aQuery3.prototype.init生成的实例化,如果没有则找init的原型,而不是aQuery3.prototype。
//console.log(aQuery3().name())
var aQuery4 = function(selector, context) {
return new aQuery4.prototype.init();
}
aQuery4.prototype = {
init: function() {
//console.log("this.init");
return this;
},
name: function() {
// console.log("this.age");
return this.age
},
age: 20
}
aQuery4.prototype.init.prototype = aQuery4.prototype;
console.log(aQuery4().name()) //20
console.log(aQuery4.prototype.name());
console