2016-08-24 38 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

相关问题