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