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;
。可能是什麼原因?
顯然!!這是原型如何在JavaScript中工作。爲了防止直接分配給Person.prototype.nationality =「XYZ」 – ajaykumar