更简单的JavaScript继承,无需重复代码
我一直在努力解决JavaScript对象/类继承的问题。我也不喜欢所有示例中重复的代码,我发现对象的名称需要编写几次 据我所知,JavaScript中的正确继承如下所示:更简单的JavaScript继承,无需重复代码,javascript,class,oop,inheritance,prototype,Javascript,Class,Oop,Inheritance,Prototype,我一直在努力解决JavaScript对象/类继承的问题。我也不喜欢所有示例中重复的代码,我发现对象的名称需要编写几次 据我所知,JavaScript中的正确继承如下所示: function Parent(v) { console.log('Parent', v); } Parent.prototype.helloParent = function() { console.log('hello parent'); } function Child(v) { Parent
function Parent(v) {
console.log('Parent', v);
}
Parent.prototype.helloParent = function() {
console.log('hello parent');
}
function Child(v) {
Parent.call( this, 'from child');
console.log('Child');
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.helloChild = function() {
console.log('hello child');
}
c = new Child();
console.log(c instanceof Child);
c.helloParent();
c.helloChild();
在这个例子中,为了扩展父对象,我必须写四次子对象,写两次父对象。我只想把它们都打一次,因为
我也不想为这种继承定义自定义函数。这让我觉得很奇怪,需要一个用户函数来实现这样一个基本的功能,阅读未知代码变得越来越困难,因为你永远不知道这个特定的继承函数到底在做什么
所以我试图找到一个更简单的版本。但是我不确定我是否错过了什么
function Parent(v) {
console.log('Parent', v);
this.helloParent = function() {
console.log('hello parent');
}
}
(Child = function(v) {
this.constructor('from child');
console.log('Child');
this.helloChild = function() {
console.log('hello child');
}
}).prototype = Parent.prototype;
c = new Child();
console.log(c instanceof Child);
c.helloParent();
c.helloChild();
这可以吗,还是有严重的缺点
编辑:关于评论,令人遗憾的是,它似乎有一些严重的缺陷。是否有其他解决方案可以减少至少多次写入父对象的名称?我使用两个非常小的函数来简化JavaScript中的继承:
function defclass(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
function extend(constructor, keys) {
var prototype = Object.create(constructor.prototype);
for (var key in keys) prototype[key] = keys[key];
return defclass(prototype);
}
其用途如下:
var Parent = defclass({
constructor: function (a) {
console.log("Parent", a);
},
helloParent: function () {
console.log("helloParent");
}
});
var Child = extend(Parent, {
constructor: function () {
Parent.call(this, "fromChild");
console.log("Child");
},
helloChild: function () {
console.log("helloChild");
}
});
最后:
var child = new Child;
console.log(child instanceof Child);
child.helloParent();
child.helloChild();
总而言之:
函数定义类原型{
var constructor=prototype.constructor;
constructor.prototype=原型;
返回构造函数;
}
函数扩展构造函数,键{
var prototype=Object.createconstructor.prototype;
对于键中的var键原型[key]=键[key];
返回原型;
}
var Parent=defclass{
构造函数:函数a{
console.logParent,a;
},
helloParent:函数{
控制台。对数对数租金;
}
};
var Child=extendedparent{
构造函数:函数{
Parent.callthis,fromChild;
console.logChild;
},
helloChild:函数{
console.loghelloChild;
}
};
var child=新的子对象;
console.logchild子实例;
儿童;helloParent;
child.helloChild javascript中的oop很难看。至少在ES6之前,它支持类并实现关键字,但即使是ES6也不支持多重继承。我为javascript编写的代码使创建类和继承更易于开发和维护。例如,要创建一个类,只需执行以下操作:
ds.make.class({
type: 'a',
constructor: function (x) { this.val = x; },
mul: function (s) {
this.val *= s;
return this;
}
});
// now to inherit class a just do this...
ds.make.class({
type: 'b',
inherits: a,
constructor: function (x) { this.val = x; },
sub: function (s) {
this.val -= s;
return this;
}
});
var o = new b(5);
var output = o.mul(3).sub(5).val; // output = 10
为什么不把这些方法放在原型上呢?如果您尝试,您可能会注意到第一个缺点。您没有进行继承,而是将Child.prototype替换为Parent.prototype,因此它们是相同的对象。这感觉更像是一次代码审查。我建议看一下我最近回答的问题。它应该可以帮助您理解缺点和其他替代方案:这里解释了原型和继承:我不明白为什么JS不提供自己的构造,使继承更易于理解,更易于作为代码阅读。这个问题的简短答案是JavaScript是一种设计糟糕的编程语言。幸运的是,负责设计该语言的人是——尽管修复程序在相当长的一段时间内不会出现。如果你想更多地了解JavaScript中的继承,那么你应该阅读我的博客文章:我只是在想,是否有可能为这个OOP东西定义自己的函数,但让它们看起来和感觉上与ES6类似?或者,如果已经有一些跨不同框架的标准定义了