您可以在不更改对象构造函数的情况下向对象构造函数添加新的动态参数吗?
Can you add a new dynamic parameter to an object constructor without changing the object constructor?
我是 Javascript 的新手,正在学习对象。我了解到您可以向具有原型的对象添加新的 属性 或方法。
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
现在我想知道是否也可以在不直接分配新 属性 或更改对象构造函数的情况下添加带有新参数的新 属性。
所以,它变成了:
function Person(first, last, age, eyecolor, nationality) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = nationality;
}
您可以通过将原始构造函数包装在一个新函数中来做到这一点,如下所示:
const originalPerson = Person;
Person = function(first, last, age, eyecolor, nationality) {
const instance = new originalPerson(first, last, age, eyecolor);
instance.nationality = nationality;
return instance;
};
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
const originalPerson = Person;
Person = function(first, last, age, eyecolor, nationality) {
const instance = new originalPerson(first, last, age, eyecolor);
instance.nationality = nationality;
return instance;
};
const joe = new Person("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);
您也可以通过继承来实现:
const originalPerson = Person;
Person = class extends originalPerson {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
};
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
const originalPerson = Person;
Person = class extends originalPerson {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
};
const joe = new Person("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);
在这两种情况下,我都重新分配了 Person
,但您不必这样做,您可以只使用 ExtendedPerson
或类似的东西:
class ExtendedPerson extends Person {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
}
...然后使用 new ExtendedPerson(/*...*/)
.
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
class ExtendedPerson extends Person {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
}
const joe = new ExtendedPerson("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);
我是 Javascript 的新手,正在学习对象。我了解到您可以向具有原型的对象添加新的 属性 或方法。
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
现在我想知道是否也可以在不直接分配新 属性 或更改对象构造函数的情况下添加带有新参数的新 属性。 所以,它变成了:
function Person(first, last, age, eyecolor, nationality) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = nationality;
}
您可以通过将原始构造函数包装在一个新函数中来做到这一点,如下所示:
const originalPerson = Person;
Person = function(first, last, age, eyecolor, nationality) {
const instance = new originalPerson(first, last, age, eyecolor);
instance.nationality = nationality;
return instance;
};
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
const originalPerson = Person;
Person = function(first, last, age, eyecolor, nationality) {
const instance = new originalPerson(first, last, age, eyecolor);
instance.nationality = nationality;
return instance;
};
const joe = new Person("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);
您也可以通过继承来实现:
const originalPerson = Person;
Person = class extends originalPerson {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
};
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
const originalPerson = Person;
Person = class extends originalPerson {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
};
const joe = new Person("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);
在这两种情况下,我都重新分配了 Person
,但您不必这样做,您可以只使用 ExtendedPerson
或类似的东西:
class ExtendedPerson extends Person {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
}
...然后使用 new ExtendedPerson(/*...*/)
.
实例:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
class ExtendedPerson extends Person {
constructor(first, last, age, eyecolor, nationality) {
super(first, last, age, eyecolor);
this.nationality = nationality;
}
}
const joe = new ExtendedPerson("Joe", "Bloggs", 42, "brown", "English");
console.log(joe.nationality);