在console中執行如下:
var?aQuery?=?function(selector)?{ ???? if?(!(this?instanceof?aQuery))?{ ???????????????console.log(this);?//window ???????????????console.log('zeze');//zeze ????????????????return?new?aQuery(selector);//疑問:這塊不是直接return了嗎?為何下面的ddd會輸出。 } ????????console.log('ddd');//ddd? var?elem?=?document.getElementById(/[^#].*/.exec(selector)[0]); this.length?=?1; this[0]?=?elem; this.context?=?document; this.selector?=?selector; this.get?=?function(num)?{ return?this[num]; }?? ????????console.log(this);?//aQuery return?this; }? aQuery('#hehe').length;?//1
2014-10-24
啊明白了,這里執行了兩次,第一次,aQuery("#hehe")調用,this是window,返回new aQuery("#hehe"),然后繼續調用方法,此時this為aQuery.