我在这里阅读了关于为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