2016-08-24 35 views
1

JSFiddle設置原型的對象的字段更改所有實例

function Person(first, last, age, eye) { 
    this.firstName = first; 
    this.lastName = last; 
    this.age = age; 
    this.eyeColor = eye; 
} 
Person.prototype = { 
    nationality : { 
     type : "" 
    }, 
    changeNationality:function(n){ 
     this.nationality.type = n; 
    } 
}; 

var myFather = new Person("John", "Doe", 50, "blue"); 

var myMother = new Person("Jane", "Doe", 50, "blue"); 

myMother.changeNationality("English"); 
myFather.changeNationality("German"); 

document.getElementById("demo").innerHTML = 
"My father is " + myFather.nationality.type + "<br/> My mother is " + myMother.nationality.type; 

。可能是什麼原因?

+0

顯然!!這是原型如何在JavaScript中工作。爲了防止直接分配給Person.prototype.nationality =「XYZ」 – ajaykumar

回答

1

因爲您正在定義nationalityprototype級別而不是Person實例,這就是prototype!的原因!這個想法是在所有類實例之間共享的,否則每個實例都會有自己的每個方法的定義,使用更多的內存和潛在的分歧行爲。

嘗試移動nationality的構造函數:

function Person(first, last, age, eye) { 
    this.firstName = first; 
    this.lastName = last; 
    this.age = age; 
    this.eyeColor = eye; 
    this.nationality = { type: "" }; 
} 
+0

但是,當我使國籍簡單的字符串,那麼每個對象都有自己的國籍。像這樣https://jsfiddle.net/d0rsasq1/7 – MOD

1

由於原型是沿着你的對象定義的每個實例共享,你應該做這樣的事情:

function Person(first, last, age, eye) { 
 
    this.firstName = first; 
 
    this.lastName = last; 
 
    this.age = age; 
 
    this.eyeColor = eye; 
 
    this.nationality = { 
 
     type : "" 
 
    }; 
 
} 
 
Person.prototype = { 
 
    
 
    changeNationality:function(n){ 
 
     this.nationality.type = n; 
 
    } 
 
}; 
 

 
var myFather = new Person("John", "Doe", 50, "blue"); 
 

 
var myMother = new Person("Jane", "Doe", 50, "blue"); 
 

 
myMother.changeNationality("English"); 
 
myFather.changeNationality("German"); 
 

 
document.getElementById("demo").innerHTML = 
 
"My father is " + myFather.nationality.type + "<br/> My mother is " + myMother.nationality.type;
<p id="demo"></p>

+0

如果原型是在每個實例中共享的,那麼當您設置原型的字符串字段時,爲什麼它不會更改其他實例。像這樣https://jsfiddle.net/d0rsasq1/7/ – MOD

+0

,因爲字符串var通過引用由值和對象var引用。 – InferOn

0

爲防止將此屬性分配給原型使用一個 級別點和字符串分配。

function Person(first, last, age, eye) { 
 
    this.firstName = first; 
 
    this.lastName = last; 
 
    this.age = age; 
 
    this.eyeColor = eye; 
 
    this.changeNationality = function(n){ 
 
     this.nationalityType = n 
 
    } 
 
} 
 

 
Person.prototype.nationalityType = 'default'; 
 

 
var myFather = new Person("John", "Doe", 50, "blue"); 
 

 
var myMother = new Person("Jane", "Doe", 50, "blue"); 
 
myMother.changeNationality("English"); 
 
myFather.changeNationality("German"); 
 
document.getElementById("demo").innerHTML = 
 
"My father is " + myFather.nationalityType + "<br/> My mother is " + myMother.nationalityType;
<p id="demo"></p>

0

改變

changeNationality:function(n){ 
     this.nationality.type = n; 
    } 

changeNationality:function(n){ 
     this.nationality = {type : n}; 
    } 

也應該工作,因爲設置屬性將創建在OB一個新JECT。但在this.nationality.type = n;的情況下

這個國際性是一個得到和做的。因此你得到的行爲。我已經更新小提琴,你可以檢查它here

相關問題