使用 JavaScript 原型调用方法
如果基方法被重写,是否可以从 JavaScript 中的原型方法调用基方法?
MyClass = function(name){
this.name = name;
this.do = function() {
//do somthing
}
};
MyClass.prototype.do = function() {
if (this.name === 'something') {
//do something new
} else {
//CALL BASE METHOD
}
};
Is it possible to call the base method from a prototype method in JavaScript if it's been overridden?
MyClass = function(name){
this.name = name;
this.do = function() {
//do somthing
}
};
MyClass.prototype.do = function() {
if (this.name === 'something') {
//do something new
} else {
//CALL BASE METHOD
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
恐怕你的例子并不像你想象的那样有效。 这部分:
覆盖 的定义
由于新创建的对象已经具有“do”属性,因此它不会查找原型链。
Javascript 中继承的经典形式很笨拙,而且很难掌握。 我建议改用 Douglas Crockfords 的简单继承模式。 像这样:
在我看来,这是在 javascript 中处理对象、构造函数和继承的更清晰的方法。 您可以在 Crockfords Javascript:优秀部分中阅读更多内容。
I'm afraid your example does not work the way you think. This part:
overwrites the definition of
Since the newly created object already has a "do" property, it does not look up the prototypal chain.
The classical form of inheritance in Javascript is awkard, and hard to grasp. I would suggest using Douglas Crockfords simple inheritance pattern instead. Like this:
In my opinion a much clearer way of handling objects, constructors and inheritance in javascript. You can read more in Crockfords Javascript: The good parts.
我知道这篇文章是 4 年前写的,但由于我的 C# 背景,我一直在寻找一种调用基类的方法,而无需指定类名,而是通过子类上的属性获取它。 所以我对 Christoph 的答案 的唯一更改是
从这个:
到这个:
I know this post is from 4 years ago, but because of my C# background I was looking for a way to call the base class without having to specify the class name but rather obtain it by a property on the subclass. So my only change to Christoph's answer would be
From this:
To this:
如果你定义一个像这样的函数(使用OOP),
有两种方法来调用原型函数:1)创建一个实例并调用对象函数:
另一种方法是... 2)直接从原型调用函数:
if you define a function like this (using OOP)
there is two ways to call a prototype function: 1) make an instance and call the object function:
and the other way is... 2) is calling the function directly from the prototype:
此解决方案使用
Object.getPrototypeOf
TestA
是具有getName
的超级TestB
是覆盖getName 的子级
但是,也有getBothNames
调用getName
的super
版本以及child
版本This solution uses
Object.getPrototypeOf
TestA
is super that hasgetName
TestB
is a child that overridesgetName
but, also hasgetBothNames
that calls thesuper
version ofgetName
as well as thechild
version替代 :
An alternative :
如果我理解正确的话,您希望始终执行基本功能,而其中一部分应该留给实现。
“模板方法”设计模式可能会为您提供帮助。
If I understand correctly, you want Base functionality to always be performed, while a piece of it should be left to implementations.
You might get helped by the 'template method' design pattern.
如果您知道您的超类的名称,您可以执行以下操作:
这将按
预期打印。
If you know your super class by name, you can do something like this:
This will print
as expected.
ES5 的另一种方法是使用 Object.getPrototypeOf(this) 显式遍历原型链
Another way with ES5 is to explicitely traverse the prototype chain using
Object.getPrototypeOf(this)
不,您需要为构造函数中的 do 函数和原型中的 do 函数指定不同的名称。
No, you would need to give the do function in the constructor and the do function in the prototype different names.
此外,如果您想要覆盖所有实例而不仅仅是某个特殊实例,那么这个可能会有所帮助。
结果:
In addition, if you want to override all instances and not just that one special instance, this one might help.
result:
我不明白你到底想做什么,但通常实现特定于对象的行为是按照以下方式完成的:
I did not understand what exactly you're trying to do, but normally implementing object-specific behaviour is done along these lines:
一种方法是保存基本方法,然后从重写方法中调用它,如下所示
Well one way to do it would be saving the base method and then calling it from the overriden method, like so