我在這裏閱讀了關於爲Javascript類定義方法Advantages of using prototype, vs defining methods straight in the constructor?,我選擇了原型方法。但我遇到了一個問題,例如:如何在類構造函數中使用原型方法
function MyClass() {};
MyClass.prototype.Hide = function() {};
function MyClass() {
this.layout = $("<div>", {id: "layout1"}).text("My content");
this.button = $("<input />", {id: "button1"});
this.layout.append(this.button);
$("#button1").click(function() {
//How can I call hide
this.Hide()//Error
});
}
MyClass.prototype.Hide = function() {
this.layout.hide("slow");
}
我該如何在構造函數中調用原型函數?我嘗試了原型方法的前向聲明,但我認爲問題在於我稱之爲this.Hide()沒有幫助!
感謝您的時間!
你有第二個解決方案是幻想,但我認爲我們都可以同意第一個更容易閱讀。 – 2011-12-16 15:11:45