如何在构造函数中访问类变量?(node . js OOP)

How to access class variables in the constructor? (node.js OOP)

本文关键字:node js OOP 类变量 构造函数 访问      更新时间:2023-09-26

是否有某种方法可以访问构造函数中的类变量?

var Parent = function() {
  console.log(Parent.name);
};
Parent.name = 'parent';
var Child = function() {
  Parent.apply(this, arguments);
}
require('util').inherits(Child, Parent);
Child.name = 'child';

。e父类的构造函数应该记录"父",子类的构造函数应该记录基于每个类的某个类变量的"子"。

这是在香草js:

var Parent = function() {
  console.log(this.name);
};
Parent.prototype.name = 'parent';
var Child = function() {
  Parent.apply(this, arguments);
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
Child.prototype.name = 'child';
var parent = new Parent();
var child = new Child();

跑龙套。继承只是简化了

Child.prototype = new Parent();
Child.prototype.constructor = Child;

util.inherits(Child, Parent);