设置原型's对象's字段更改所有实例
Setting prototype's object's field changes all instances
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;
当我改变父亲的国籍时,也会改变母亲的国籍。原因是什么?
因为您在prototype
级别定义nationality
,而不是Person
实例,所以这就是prototype
的原因!这个想法是在所有类实例之间共享,否则每个实例都会对每个方法有自己的定义,使用更多的内存和潜在的发散行为。
尝试将nationality
移动到构造函数:
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.nationality = { type: "" };
}
由于原型与对象定义的每个实例共享,因此应该执行以下操作:
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>
为了防止这种情况,请使用之一将属性分配给原型级别点和字符串分配
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>
更改
changeNationality:function(n){
this.nationality.type = n;
}
至
changeNationality:function(n){
this.nationality = {type : n};
}
也应该工作,因为设置属性将在对象上创建一个新的属性。但在this.nationality.type = n;
的情况下
this.nationality是一个正在执行的类型,因此它将从它获得的位置(即从原型(分配值。因此,你得到了这种行为。我已经更新了小提琴,你可以在这里查看
相关文章:
- 使用jQuery获取Dropzone实例/对象
- 在requirejs中共享实例化对象
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- 代码挑战:创建一个跟踪对象实例总数的类Foo
- JavaScript:如果现有对象被实例化为具有特定值,如何创建警报
- 用javascript创建另一个对象的实例
- Backbone relational无法实例化两个RelationalModel对象
- 创建一个类,该类使用es6类语法将Function对象创建为实例
- 为什么属性存在于对象实例上,即使其原型发生了更改
- 如何在angularJS中获得对指令对象实例的引用
- 无法将值从实例对象传输到父类的实例数组中
- Javascript 原型 - 不仅适用于实例对象
- 如何在 angular 的控制器中使用用户定义的类实例/对象
- 三.js不是三的实例.对象3D
- 实例对象的私有状态的以下实现存在缺陷
- ES6 setter -保持对象干净//映射实例对象上的参数
- 直接从空格键访问模板实例对象属性
- 理解AngularJS $resource()返回什么(类或实例对象)
- 在Chrome原生消息传递:有多长是本地应用实例对象的生命周期
- 集合/实例对象样式为没有proto的vie javascript