jQuery jQuery
分离构造器
http://www.imooc.com/code/3398
var $$ = ajQuery = function(selector) { this.selector = selector; return this } ajQuery.fn = ajQuery.prototype = { selectorName:function(){ return this.selector; }, constructor: ajQuery } var a = new $$('aaa'); //实例化 console.log(a); var name = a.selectorName();//aaa //得到选择器名字 console.log(name);
改进,去掉new
ar $$ = ajQuery = function(selector) { console.log(this); if(!(this instanceof ajQuery)){ return new ajQuery(selector); } this.selector = selector; return this; } ajQuery.fn = ajQuery.prototype = { selectorName:function(){ return this.selector; }, constructor: ajQuery } var a = $$('aaa'); //实例化 console.log(a); var name = a.selectorName();//aaa //得到选择器名字 console.log(name);