Javascript 如何允许对象从其类继承成员?
这个问题是关于Javascript的,但是我学的第一种编程语言是Java,所以我最容易理解它 在Java中,对象可以访问静态成员,如Oracle的文章所示:Javascript 如何允许对象从其类继承成员?,javascript,java,static-members,prototype-programming,Javascript,Java,Static Members,Prototype Programming,这个问题是关于Javascript的,但是我学的第一种编程语言是Java,所以我最容易理解它 在Java中,对象可以访问静态成员,如Oracle的文章所示: public class Bicycle { // INSTANCE fields private int cadence; private int gear; private int speed; // CONSTRUCTOR constructor Bicycle(c,g,s) { /*...*/ } /
public class Bicycle {
// INSTANCE fields
private int cadence;
private int gear;
private int speed;
// CONSTRUCTOR
constructor Bicycle(c,g,s) { /*...*/ }
//...
// STATIC fields
private static int numberOfBicycles = 0;
}
还可以使用对象引用引用静态字段,如
myBike.numberOfBicycles
除了可以访问静态字段的对象外,子类也可以访问静态字段,如中所述
MountainBike
现在有4个实例字段:cadence
、gear
、speed
和seatHeight
;和两个静态字段:numberOfBicycles
,和mountainBikePrice
然而,我想在我的Javascript程序中模拟这种行为,因为JS是面向原型的,而不是面向对象的,所以对象和子类不能访问它们的“类”成员
如果我调用MountainBike.numberOfBicycles
我会得到undefined
(这在Java中不会发生,它会返回父级的值)
除了分配Bicycle.prototype.numberOfBicycles=0
,对象必须有更简单的方法继承其类的属性,子类必须继承其父类的属性
myBike.class
?如果是这样,那么我可以调用myBike.class.numberOfBicycles
MountainBike.super
实际上,您要查找的属性是
constructor
(如myBike.constructor.numberOfBicycles
中所示)
对象的构造函数(某种程度上)类似于Java中实例的类。可能最好对此持保留态度,因为这些概念并不完全可以互换
编辑:
因此,我看到您基本上是在尝试使用原型和构造函数技术(这很好)在JavaScript中创建一些完整的“类”实现。主干在其功能中有一个有趣的方法。相关来源如下(有关此代码的许可证,请参见问题底部):
注释是我的。我编辑了代码,以暴露与OP发布的内容的一些相似之处(即使用Object.create
而不是\uuu.create
)。使用方法是在父级parent.extend=extend中设置
然后使用child=Parent.extend({…newproperties and methods…},…)
创建子类
我决定继续发布整个函数,因为它可能包含您尚未询问的问题的解决方案(例如构造函数的继承)特别是对于问题2,骨干开发人员决定使用\uuuuu super\uuuu
显式保存子类中的父类,因此您也可以在继承代码中这样做(在调用对象后。创建
)
将ES2015类和继承与扩展一起使用可能更有意义(如下所示)。对它的支持不是很好,因此您需要使用预处理器,例如。以下是这些类别的一个示例:
class Parent {...}
class Child extends Parent {...}
已发布主干代码片段的许可证:
(c) 2010-2016 Jeremy Ashkenas,DocumentCloud和调查记者与编辑。
主干网可根据MIT许可证自由分发。
有关所有详细信息和文档:
谢谢你的回答。这是正确的,但在测试中我意识到我问错了问题!(好的,只是部分。)我更新并添加了另一个问题,正确的一个,涉及子类继承。@chharvey看看我的编辑……希望它能帮助更多
// constructor
function Bicycle(c,g,s) {
this.cadence = c
this.gear = g
this.speed = s
}
// instance methods
Bicycle.prototype.go = function () { /*...*/ }
Bicycle.prototype.stop = function () { /*...*/ }
// STATIC field
Bicycle.numberOfBicycles = 0 // <-- I want THIS field inherited
// subclass constructor
function MountainBike(c,g,s,h) {
Bicycle.call(this,c,g,s)
this.seatHeight = h
}
// vvvvvvvv extension mechanism... ignore this vvvvvvvvvv
MountainBike.prototype = Object.create(Bicycle.prototype)
MountainBike.prototype.constructor = MountainBike
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// STATIC field
MountainBike.mountainBikePrice = 324
var extend = function(protoProps, staticProps) {
var parent = this;
var child;
// Inherits constructor if not specified in child props.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// copy all properties from parent and from static properties
// into child
_.extend(child, parent, staticProps);
// figure out child prototype (kind of like you had)
child.prototype = Object.create(parent.prototype);
_.extendOwn(child, protoProps);
child.prototype.constructor = child;
// Save super for later access (answers question 2)
child.__super__ = parent.prototype;
return child;
};
class Parent {...}
class Child extends Parent {...}