// Q returns new Library object
var Q = function (params) {
return new Library(params);
};
// In our Library we get our selector with querySelectorAll (we do not support < ie8)
// We also add selector length, version and twitter/github or whatever you like as information about your library.
var Library = function (params) {
// Get params
var selector = document.querySelectorAll(params),
i = 0;
// Get selector length
this.length = selector.length;
this.version = '0.1.0';
this.twitter = 'http://www.twitter.com/mmmmm_';
// Add selector to object for method chaining
for (; i < this.length; i++) {
this[i] = selector[i];
}
// Return as object
return this;
};
爲什麼我們在構造函數的末尾有return this;
?如果我們忽略它會發生什麼?在構造函數定義中「return this」:它的作用是什麼?
這不是第一次,我覺得在構造函數中該指令,但有時它的出現,有時(在其他類其他腳本),它不會出現,我不明白其中的道理。
從函數返回當前對象。 –