Javascript - 从子方法继承和调用父方法
Javascript - Inheritance and calling parent methods from child
我在JavaScript中与OOP斗争了几天,但我没有找到解决方案。
我创建了 3 个对象,一个超级类、一个子类和一个inheritance_manager,它们应该在我的子类上做所有的"原型"魔法。
继承管理器:
Inheritance_Manager = {};
Inheritance_Manager.extend = function(subClass, baseClass) {
function inheritance() {
}
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass;
subClass.superClass = baseClass.prototype;
};
超级(父)类:
SDSection = function(sectionId, resourceId) {
this.self = this;
this.sectionId = sectionId;
this.resourceId = resourceId;
...
};
SDSection.prototype.doSetups = function() {
...
};
子班:
TypedSectionHandler = function(sectionId, resourceId) {
SDSection.call(this, sectionId, resourceId);
...
};
Inheritance_Manager.extend(TypedSectionHandler, SDSection);
TypedSectionHandler.prototype.doSetups = function() {
...
SDSection.doSetups.call(this);
...
};
我想做的是在其他编程语言(如php或java)中很简单。我想从类型为"TypedSectionHandler"的子类中的"doSetups"方法调用父类"SDSection"中覆盖的"doSetups"方法。
我已经为这个问题苦苦挣扎了大约 1 周,我尝试了不同的解决方案,从基本到更复杂的解决方案,但似乎没有任何效果。
每次执行脚本时,例如在chrome或Firefox中,我都会收到错误"无法在未定义上调用方法调用"或更简单,未定义"SDSection.doSetups"。
至少我从这里选择了上述方法并使其适应我的需求,但它无论如何都不起作用,并且浏览器因相同的错误退出。慢慢地,我变得非常疯狂。:)
有人知道我做错了什么以及有效的解决方案会是什么样子吗?
谢谢
乌多
调用作为父类prototype
一部分的 doSetups
函数。
TypedSectionHandler.prototype.doSetups = function() {
...
SDSection.prototype.doSetups.call(this);
...
};
如果你想看看不同的继承技术(特别是不需要调用者知道父类型的技术),你可能想看看 CoffeeScript 的 __extends
函数,以及它如何执行继承。
如果你正在做纯JavaScript,请使用voithos的解决方案。 我喜欢在我当前的项目中使用 John Resig 的简单继承片段。 缩小后只有 521 字节,没有依赖项,在您的情况下,它会像这样工作:
SDSection = Class.extend({
init: function(sectionId, resourceId) {
this.sectionId = sectionId;
this.resourceId = resourceId;
...
},
doSetups: function(){
...
}
});
TypedSectionHandler = SDSection.extend({
doSetups: function(){
...
this._super();
...
}
});
如果你想自己做,你可以像voithos说的那样 - 自己编写它可以帮助你理解js OOP。
如果你想有更多的舒适性(=不必使用apply
和call
- 如果你在Visual Studio 2012中开发 - 基方法/构造函数智能感知支持)我想让你看看我写的一个框架:jsframework
有了它,您可以非常轻松地编写示例:
SDSection = new Class(Object, function(base, baseConstructor)
{
// member fields for prototype
this.self = null;
this.sectionId = -1;
this.resourceId = -1;
// constructor
this.init = function(sectionId, resourceId)
{
this.self = this;
this.sectionId = sectionId;
this.resourceId = resourceId;
};
// member functions
this.doSetups = function()
{
console.log("SDSection doSetups: " + this.sectionId + "/" + this.resourceId);
};
});
TypedSectionHandler = new Class(SDSection, function(base, baseConstructor)
{
this.anotherId = -2;
// constructor
this.init = function(sectionId, resourceId, anotherId)
{
baseConstructor(sectionId, resourceId);
this.anotherId = anotherId;
};
// member functions
this.doSetups = function()
{
// call base function
base(this).doSetups();
console.log("TypedSectionHandler doSetups: " + this.anotherId);
};
});
var t = new TypedSectionHandler(1, 2, 3);
t.doSetups();
console.log(t instanceof TypedSectionHandler);
console.log(t instanceof SDSection);
这是一个工作 jsfiddle: http://jsfiddle.net/QYXSr/1/
输出:
SDSection doSetups: 1/2
TypedSectionHandler doSetups: 3
true
true
免責聲明:
正如我所说,我是这个框架的开发者;)
- 未捕获错误:无法在初始化之前调用方法;
- 通过ajax从客户端调用C#方法来执行C#方法
- 如何避免在angular上多次调用方法;s ng重复
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 从window.onbeforeunload调用方法背后的代码
- 当输入字段为空时,如何在angular中调用方法
- 函数调用方法有什么用
- 多次调用方法后返回相同promise的模式
- 当从Chrome扩展动态注入JS时,从onload()内部调用方法
- 茉莉花 - 未调用方法
- jQuery如何在原型中调用方法
- ng显示“;调用方法“;不起作用
- Odoo销售点如何访问模型并使用JS调用方法
- 从React调用方法.JS州
- TinyMCE验证给出错误:无法调用方法'getContent'的未定义
- 向模板实例变量传递调用方法调用的结果时出现异常
- 如何在对象中调用方法
- 检查对话框是否为 Open 会引发“初始化前无法在对话框上调用方法”错误
- 在输入类型提交按钮上调用 C# 方法
- 通过类选择器单独调用方法