Javascript继承,可访问';超类';?
为了理解如何在Javascript中实现继承,我偶然发现了许多不同的实现,包括Crockford、Resigs、Javascript继承,可访问';超类';?,javascript,inheritance,smalltalk,super,Javascript,Inheritance,Smalltalk,Super,为了理解如何在Javascript中实现继承,我偶然发现了许多不同的实现,包括Crockford、Resigs、Prototype、klass,等等 我错过的(我准备好迎接这场喧嚣)是一对小巧玲珑的self/super:self扮演着与this类似的角色,即代表当前的“对象”,而super指的是this的一个只包含超类的版本 [跳到“]”如果您知道super在Smalltalk中做了什么:假设子类覆盖了超类中定义的method1,我仍然可以使用子类.method2()中的访问超类实现。这将不会执
Prototype
、klass
,等等
我错过的(我准备好迎接这场喧嚣)是一对小巧玲珑的self/super:self
扮演着与this
类似的角色,即代表当前的“对象”,而super
指的是this
的一个只包含超类的版本
[跳到“]”如果您知道super
在Smalltalk中做了什么:假设子类
覆盖了超类
中定义的method1
,我仍然可以使用子类.method2()
中的访问超类实现。这将不会执行子类.method1()
代码
function Superclass () {
}
Superclass.prototype.method1 = function () {
return "super";
}
function Subclass () {
}
Subclass.prototype.method1 = function () {
return "sub";
}
Subclass.prototype.method2 = function () {
alert (super.method1 ());
}
var o = new Subclass;
o.method2 (); // prints "super"
]
有没有“Javatalk”软件包?到目前为止,我只看到Javascript中的OO模拟,它允许访问当前定义的方法(method2
)的超类实现,而没有任何其他方法(例如method1
)
谢谢,nobi,长话短说:是我读过的最好的JavaScript教程。所以我可以把它推荐给你。祝你好运 JavaScript中没有super
功能
了解超类后,可以使用以下命令直接调用super方法:
如果您想模拟一个通用的super
(我不提倡这样做),您可以使用以下方法:
function sup(obj, name) {
var superclass = Object.getPrototypeOf(Object.getPrototypeOf(obj));
return superclass[name].apply(obj, [].slice.call(arguments,2));
}
你会用它作为
sup(this, 'method1');
而不是你的
super.method1();
如果您要传递参数:
sup(this, 'method1', 'some', 'args');
而不是
super.method1('some', 'args');
请注意,这假设您使用
Subclass.prototype = new Superclass();
有很多方法可以在JavaScript中实现super
功能。例如:
function SuperClass(someValue) {
this.someValue = someValue;
}
SuperClass.prototype.method1 = function () {
return this.someValue;
};
function SubClass(someValue) {
//call the SuperClass constructor
this.super.constructor.call(this, someValue);
}
//inherit from SuperClass
SubClass.prototype = Object.create(SuperClass.prototype);
//create the super member that points to the SuperClass prototype
SubClass.prototype.super = SuperClass.prototype;
SubClass.prototype.method2 = function () {
alert(this.super.method1.call(this));
};
var sub = new SubClass('some value');
sub.method2();
编辑:
下面是一个非常通用的super
方法的示例,该方法依赖于非标准特性我真的不推荐这个,它只是作为学习的目的。
Object.prototype.super = function () {
var superProto = Object.getPrototypeOf(Object.getPrototypeOf(this)),
fnName = arguments.callee.caller.name,
constructorName = this.constructor.name;
if (superProto == null) throw constructorName + " doesn't have a superclass";
if (typeof superProto[fnName] !== 'function') {
throw constructorName + "'s superclass ("
+ superProto.constructor.name + ") doesn't have a " + fnName + ' function';
}
return superProto[arguments.callee.caller.name].apply(
this,
[].slice.call(arguments, 1)
);
};
function A() {
}
A.prototype.toString = function toString() {
//call super method Object.prototype.toString
return this.super();
};
var a = new A();
console.log(a.toString());
您的instead of是错误的,它更多地代替了super.method1.call(this)
@plalx My“而不是”不是合适的javascript,但请参考OP的问题。只是他可能认为它们在逻辑上是等价的。@dystroy我将尝试您的方法和标记为答案的方法-标记为另一种方法只是因为它在源代码中看起来更好--sup(这个,“方法”)
很难看,我认为。谢谢这看起来确实值得,我还没看过引用许多不同语言的文章。我来看看,我想,你会喜欢的:)祝你好运!Javascript的一大难题——“有很多种方法”:-,所以现在是时候编写我自己的子类函数了!如此多次重复的超类
和子类
和原型
——看起来一点也不枯燥。。。谢谢anyway@virtualnobi特别是如果您将继承过程封装在函数中,那么您肯定可以实现这一点。你可以把它推到极端,使用一个非常通用的super
方法,但我不推荐这样做。
Object.prototype.super = function () {
var superProto = Object.getPrototypeOf(Object.getPrototypeOf(this)),
fnName = arguments.callee.caller.name,
constructorName = this.constructor.name;
if (superProto == null) throw constructorName + " doesn't have a superclass";
if (typeof superProto[fnName] !== 'function') {
throw constructorName + "'s superclass ("
+ superProto.constructor.name + ") doesn't have a " + fnName + ' function';
}
return superProto[arguments.callee.caller.name].apply(
this,
[].slice.call(arguments, 1)
);
};
function A() {
}
A.prototype.toString = function toString() {
//call super method Object.prototype.toString
return this.super();
};
var a = new A();
console.log(a.toString());