為何不用instanceof的方式來實現構造器的無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
舉報
2018-03-04
好像是因為這樣可以少用個if,減少代碼長度增加性能。。