[英]undefined result using prototype [javascript]
所以我正在學習使用javascript的原型,並嘗試了一些代碼:
function Employee(name) { this.name= name; }
var m = new Employee("Bob");
var working= { isWorking: true };
Employee.prototype = working;
alert(m.isWorking);
不幸的是,我收到了一條未定義的消息,而不是真實的值。 有這個結果的原因嗎?
我做了幾次測試。 我得出的結論是,重新分配原型對象會導致以前創建的Employee類的所有實例無法訪問在新分配的原型內找到的任何屬性。 這個准確嗎?
更改原型不會影響已經創建的對象。 它只會影響基於該對象創建的對象。
有一個屬性__proto__
可以用來更改原型,但是不需要實現。 ES6確實定義了setPrototypeOf
方法來更改原型,但是由於僅在ES6中支持,因此可能有所不同。
簡單的解決方法是正確分配它。
function Employee(name) {
this.name = name;
}
var m = new Employee("Bob");
var working = {
isWorking: true
};
Employee.prototype.working = working;
alert(m.working.isWorking);
對於MULTIPLE員工,更好的解決方法是創建一個類,然后創建該類的實例:在此處進行操作: http : //jsfiddle.net/MarkSchultheiss/p6jyqbgv/1/
"use strict";
function makeClassStrict() {
var isInternal, instance;
var constructor = function(args) {
if (this instanceof constructor) {
if (typeof this.init == "function") {
this.init.apply(this, isInternal ? args : arguments);
}
} else {
isInternal = true;
instance = new constructor(arguments);
isInternal = false;
return instance;
}
};
return constructor;
}
var EmployeeClass = makeClassStrict();
EmployeeClass.prototype.init = function(employeeName, isWorking) {
var defaultName = 'notbob';
this.name = employeeName ? employeeName : defaultName;
this.working = !!isWorking;
};
// call this to get the name property
EmployeeClass.prototype.getName = function() {
return this.name
};
//note no "new" needed due to the makeClassStrict that does that
var m = EmployeeClass("Bob");
alert(m.working +":"+ m.name);
m.working = true;
alert(m.working +":"+ m.name);
var notbob = EmployeeClass("Charlie",false);
alert(notbob.working +":"+ notbob.name);
alert(notbob.getName()+ m.getName());
首先,在設置原型之前,您已經創建了Employee
的實例,這樣該對象將不會繼承新的原型值。
接下來,設置原型后創建的所有對象都將繼承新的原型對象。
最后,該對象將具有isWorking
屬性,而不是working
屬性。
因此,請重做您的示例:
function Employee(name) { this.name= name; };
var m1 = new Employee("Bob");
var working= { isWorking: true };
Employee.prototype = working;
var m2 = new Employee("Sam");
alert(m1.isWorking); // undefined
alert(m2.isWorking); // true
您不能覆蓋整個原型屬性,並且不能期望已經存在的實例正常工作。 JavaScript不能那樣工作。 但是您可以遍歷原型對象並取消設置任何已設置的對象,然后遍歷新對象並將其設置為其他對象。
function Employee(name) { this.name= name; }
var m = new Employee("Bob");
var working= { isWorking: true };
for(var j in Employee.prototype){delete Employee.prototype[j];}//unset all properties, the same as setting to {}
for(j in working){Employee.prototype[j]=working[j];}//set the properties
alert(m.isWorking);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.