Javascript继承,可以访问“超类”中的*所有*方法
Javascript inheritance with access to *all* methods in 'superclass'?
为了理解如何在Javascript中完成继承,我偶然发现了许多不同的实现,包括Crockfords,Resigs,Prototype
,klass
等。
错过的(我为轩然做好准备)是Smalltalkish自我/超级对:self
扮演着与this
类似的角色,即代表当前的"对象",super
指的是this
的超类版本。
[跳到"]"如果你知道super
在Smalltalk中做了什么:假设Subclass
已经覆盖了Superclass
中定义的method1
,我仍然可以在Subclass.method2()
中使用super.method1()
访问超类实现。这不会执行Subclass.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
)。
谢谢,诺比
JavaScript 中没有super
功能。
当你知道超类时,你可以使用 call 直接调用超方法:
Superclass.method1.call(this);
如果你想模仿一个通用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());
Okey,长话短说:这是我读过的最好的JavaScript教程。所以我可以向你重新评论。祝你好运!
相关文章:
- 我如何找到一个句子中的所有空格并替换忽略它们
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 使用Jquery选择或取消选择ListView中的所有复选框
- AngularJs对所有页面中的所有记录进行排序
- JavaScript:只有当数组中的所有项都为true时才执行函数
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 如何知道javascript for语句中的所有结果是否都是错误的
- 使用jQuery预加载目录中的所有图像
- 使用Jquery查找asp.net GridView中的所有复选框
- 如何在不使用require语句的情况下使用webpack加载目录中的所有文件
- 在 JavaScript 数组中的所有元素之间穿插元素的简洁方法
- 在underscore.js中显示列表中的所有项目(使用Parse.com)
- 如何将onChange方法添加到ExtJS 4表单中的所有字段(textField)中
- 替换字符串javascript中的所有特殊字符..
- 在PHP中的所有页面上都包含页眉
- 如何使文本自动出现在HTML页面中的所有块引号之后
- Javascript/webpack:如何用自定义的文件循环连接目录中的所有json文件
- 使用moment.js获取月份和年份中的所有日期
- 使用JavaScript或Prototype获取表单中的所有控件并启用它们