我想通過閱讀源學習MooTools的,我不明白爲什麼它使功能的本地副本:Mootools的代碼解釋
var Function = this.Function;
但爲什麼它不會使陣列的本地副本,數字和字符串做同樣的事情,例如他們第一次出現被直接分配給,所以爲什麼對待功能不同?
Function.from = function(item){
return (typeOf(item) == 'function') ? item : function(){
return item;
};
};
Array.from = function(item){
if (item == null) return [];
return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : slice.call(item) : [item];
};
Number.from = function(item){
var number = parseFloat(item);
return isFinite(number) ? number : null;
};
String.from = function(item){
return item + '';
};
另外我不明白如何行149函數調用存儲在其本地原型屬性的實現函數?
Function.implement({
hide: function(){
this.$hidden = true;
return this;
},
protect: function(){
this.$protected = true;
return this;
}
});
是因爲Function是一個函數,所以它的內部[[prototype]]是Function.prototype?
關於第149行的更新,這是因爲'實現'位於'Function.prototype'上,它自動使其可用於其原型鏈中具有'Function.prototype'的所有函數。這包括構造函數,包括'Function'構造函數。 – 2012-06-08 21:38:03
執行此操作...'Function.prototype === Object.getPrototypeOf(Function)',您會看到'Function'實例在其原型鏈中有其自己的'prototype'對象。你會得到相同的結果,你創建的函數...'Function.prototype === Object.getPrototypeOf(function(){})'http://es5.github.com/#x15.3.3 – 2012-06-08 21:39:52
hide/protect是由Class使用的函數裝飾器 - 它們允許方法變成'private',因爲類構造器對象中的所有函數屬性都是'wrapped',並且如果保護標誌打開,它不會讓它在外部調用等等。除了Class本身,裝飾器沒有任何意義。查看MooTools的「類型」 - 例如。 https://github.com/mootools/mootools-core/blob/master/Source/Types/Function.js –