我一直在嘗試javascript中的一些基礎知識,這裏是我觀察到的。在Javascript中使用函數分配進行原型設計
我寫函數對象的原型修改方法
Function.prototype.method = function(name, func)
{
this.prototype[name] = func;
return this;
}
function pirates(value)
{
console.log("I just throw away the value!!" + value);
}
pirates.method("my_skill", function(){
console.log("Pirate skills");
});
new_pirate = new pirates(1234);
//SUCCESS
new_pirate.my_skill(); //prints "Pirate skills"
var someCrappyVariable = function(){
return function()
{
console.log("I am going to just sit and do nothing. Really!");
}
}();
**//Throws error. WHY???????? This was assigned a function, so ideally prototype should work on this too? Shouldn't it?**
someCrappyVariable.method("crappyFunction", function(){
console.log("am I doomed?");
});
爲什麼最後分配引發一個錯誤someCrappyVariable不是一個函數,當它被賦予的功能較早參考?我很困惑。
乾杯
嘗試執行'crappyFunction'方法時出現錯誤 – 2009-12-28 21:04:09