為何不用instanceof的方式來實(shí)現(xiàn)構(gòu)造器的無new形式而用init?
var $$ = ajQuery = function(selector) {
? ?if(!(this instanceof ajQuery)){
? ? ? ?return new ajQuery(selector);
? ?}
? ?this.selector = selector;
? ?return this
}
這種不是更直接嗎?
var $$ = ajQuery = function(selector) {
? ?if(!(this instanceof ajQuery)){
? ? ? ?return new ajQuery(selector);
? ?}
? ?this.selector = selector;
? ?return this
}
這種不是更直接嗎?
2018-03-03
舉報(bào)
2018-03-04
好像是因?yàn)檫@樣可以少用個(gè)if,減少代碼長度增加性能。。