[英]How to add a new property to an existing prototype/construction function along with its parameter
這僅用於教育目的,比如我有一個現有的原型/構造函數:
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');
有任何想法嗎?
首先,讓我們澄清原型與構造函數不同。 你展示的是一個構造函數。 此函數將用於構造符合您創建的接口的新對象,但它也將繼承底層對象的屬性 - 該對象是原型。
// 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.