这仅用于教育目的,比如我有一个现有的原型/构造函数:
var Dog = function(name, age, color){
this.name = name;
this.age = age;
this.color = color;
};
var pitBull = new Dog('Rocky', 5, 'black');
我想添加(通过编码)一个名为type
的新参数,如下所示:
var Dog = function(name, age, color, type){
// code goes here.. };
同时我想添加一个具有确切名称的新属性,如下所示:
this.type = type;
所以我可以这样做:
var pitBull = new Dog('Rocky', 5, 'black', 'PitBull');
任何想法?
答案 0 :(得分:0)
首先,让我们澄清原型与构造函数不同。你展示的是一个构造函数。此函数将用于构造符合您创建的接口的新对象,但它也将继承底层对象的属性 - 该对象是原型。
// This is your constructor function that takes arguments that help
// it construct the instance properties:
var Dog = function(name, age, color){
// These are the instance properties
this.name = name;
this.age = age;
this.color = color;
};
// And, here you are constructing a new instance of Dog
var pitBull = new Dog('Rocky', 5, 'black');
"我想添加(通过编码)一个名为type的新参数,就像这样"
var Dog = function(name, age, color, type){
// code goes here.. };
更改构造函数的基本参数结构后,确实不是正确的方法。您应该做的是使用新的构造函数创建一个新的子类型,并使新类型继承旧类型。新类型将采用所有4个参数,但它只会设置最新的参数,它会将其他3个参数传递给它继承的基础对象。
"同时我想添加一个名为"
的新属性没问题:
// Make new constructor function
function BetterDog(name, age, color, type){
this.type = type; // New property based on new parameter value
// Now pass the original 3 parameters to the prototype
// and let that object will handle the arguments as normal
Dog.prototype.constructor.call(this, name, age, color);
}
// Now, we'll set this new object to inherit from the earlier one by setting
// it's prototype to a new instance of a Dog. A new BetterDog is now going
// to inherit all the properties of Dog
BetterDog.prototype = new Dog();
// But, one problem here is that when we try to make a new instance of
// BetterDog, the Dog constructor function will be called. This happens
// because we switched the prototype. We can fix that like this:
BetterDog.constructor = BetterDog;
// Finally, we can construct a new BetterDog with all 4 parameters:
var pitBull = new BetterDog('Rocky', 5, 'black', 'PitBull');
整个事情在一起:
// This is your constructor function that takes arguments that help
// it construct the instance properties:
var Dog = function(name, age, color){
// These are the instance properties
this.name = name;
this.age = age;
this.color = color;
};
// Make new constructor function
function BetterDog(name, age, color, type){
this.type = type;
// Now pass the original 3 parameters to the prototype
// and let that object will handle the arguments as normal
Dog.prototype.constructor.call(this, name, age, color);
}
// Now, we'll set this new object to inherit from the earlier one by setting
// it's prototype to a new instance of a Dog. A new BetterDog is now going
// to inherit all the properties of Dog
BetterDog.prototype = new Dog();
// But, one problem here is that when we try to make a new instance of
// BetterDog, the Dog constructor function will be called. This happens
// because we switched the prototype. We can fix that like this:
BetterDog.constructor = BetterDog;
// Finally, we can construct a new BetterDog with all 4 parameters:
var pitBull = new BetterDog('Rocky', 5, 'black', 'PitBull');
// Test:
console.log(pitBull.name);
console.log(pitBull.age);
console.log(pitBull.color);
console.log(pitBull.type);