我想知道如何命名我的主构造函数将使用的构造函数,但没有继承。解释:
我有一个构造函数 Human:
function Human() {}
人类有 ARM 、腿等属性:
function Human() {
this.arms = new Arms();
this.legs = new Legs();
}
我想知道如何根据 ARM 和腿的常见用法和惯例来命名。如果我应该使用:
function Human() {
this.arms = new HumanArms();
this.legs = new HumanLegs();
}
或者:
function Human() {
this.arms = new Human.Arms();
this.legs = new Human.Legs();
}
最佳答案
只是一个建议,您应该考虑使用 es6 class 关键字和 static 关键字
class Arms {
constructor() {}
}
class Legs {
constructor() {}
}
class Human {
constructor() {
this.arms = Human.Arms();
this.legs = Human.Legs();
}
static Arms() {
return new Arms();
}
static Legs() {
return new Legs();
}
}
但这一切都取决于个人选择
关于javascript - 命名作为主类属性的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423193/