在console中執(zhí)行如下:
var?aQuery?=?function(selector)?{ ???? if?(!(this?instanceof?aQuery))?{ ???????????????console.log(this);?//window ???????????????console.log('zeze');//zeze ????????????????return?new?aQuery(selector);//疑問(wèn):這塊不是直接return了嗎?為何下面的ddd會(huì)輸出。 } ????????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
啊明白了,這里執(zhí)行了兩次,第一次,aQuery("#hehe")調(diào)用,this是window,返回new aQuery("#hehe"),然后繼續(xù)調(diào)用方法,此時(shí)this為aQuery.