JavaScript 中的超类方法

发布于 2024-08-03 18:33:03 字数 996 浏览 3 评论 0原文

我正在用 javascript 编写一些对象(我猜是类)。 B 类继承自 A 类。A 类有一个名为 isValid 的方法,B 类重写该方法。我正在使用 YUI 扩展函数让 B 类扩展 A 类。

A = function(){
}
A.prototype = {
   isValid:function(){
       /* Some logic */
       return booleanValue;
   }
}

B = function(){
}

YAHOO.lang.extend(B, A,{
     isValid:function(){
        // call class A's valid function
        // some more logic for class B.
        return booleanValue;
     }
});

我想要做的是在 B 类的 isValid 函数内调用 A 类的 isValid 函数。问题是,我可以从类 B 的 isValid 方法访问类 A 的 isValid 方法吗?我知道您可以使用以下行从类 B 的构造函数内部访问类 A 的构造函数:

this.constructor.superclass.constructor.call(this,someParam);

方法是否可能有类似的情况?如果没有,这样做的良好做法是什么?目前,我正在创建一个在超类的 isValid 方法内部调用的辅助方法,

A.prototype = {
    a_isValid:function(){
       // class A's is valid logic
       return booelanValue;
    },
    isValid:function() {return this.a_isValid();}
}

然后我可以从类 B 调用 a_isValid 函数。这对我来说确实有用,但如果可能的话,我更愿意直接调用超类的 isValid 函数。

I am writing some objects (classes I guess) in javascript. Class B inherits from Class A. Class A has a method called isValid, and class B overrides that method. I am using the YUI extend function to have Class B extend Class A.

A = function(){
}
A.prototype = {
   isValid:function(){
       /* Some logic */
       return booleanValue;
   }
}

B = function(){
}

YAHOO.lang.extend(B, A,{
     isValid:function(){
        // call class A's valid function
        // some more logic for class B.
        return booleanValue;
     }
});

What I want to be able to do is call Class A's isValid function inside class B's isValid function. The question is, can I access class A's isValid method from class B's isValid method? I know that you can access class A's constructor from inside Class B's constructor with the following line

this.constructor.superclass.constructor.call(this,someParam);

Is something similar possible for methods? If not, what is a good practices for doing this? Currently I am making a helper method called inside the super class' isValid method

A.prototype = {
    a_isValid:function(){
       // class A's is valid logic
       return booelanValue;
    },
    isValid:function() {return this.a_isValid();}
}

Then I can call the a_isValid function from class B. This does work for me but I would prefer to call the super class' isValid function directly if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-08-10 18:33:03

来自 YUI 文档:

YAHOO.lang.extend(YAHOO.test.Class2, YAHOO.test.Class1); 
YAHOO.test.Class2.prototype.testMethod = function(info) { 
// chain the method 
YAHOO.test.Class2.superclass.testMethod.call(this, info); 
alert("Class2: " + info); 
}; 

它对你不起作用吗?第 4 行应该调用 Class1 的(超类)testMethod。

From YUI docs:

YAHOO.lang.extend(YAHOO.test.Class2, YAHOO.test.Class1); 
YAHOO.test.Class2.prototype.testMethod = function(info) { 
// chain the method 
YAHOO.test.Class2.superclass.testMethod.call(this, info); 
alert("Class2: " + info); 
}; 

Doesn't it work for you? The 4th line should call Class1's (superclass) testMethod.

锦爱 2024-08-10 18:33:03

我出于文档目的发布了另一种方法。

如果 messageFormController 派生于 formController,则可以将 super.setView 调用为:

messageFormController.setView = function setView(element) {
    formController.setView.bind(this)(element);
    // Additional stuff
};

I am posting another approach for documentation purposes.

If messageFormController derives of formController, you call super.setView as:

messageFormController.setView = function setView(element) {
    formController.setView.bind(this)(element);
    // Additional stuff
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文