JavaScript Prototype (一)

JS中对象赋值既是引用(Reference),赋值之后,修改这个,就会引起原来那个变量的变化.
但是使用prototype属性,将一个变量作为龙一个变量的prototype这样修改就不会影响到作为原型的对象的属性值
有助于我们学习prototype(原型)

1
2
3
4
5
6
7
8
9
var stooge = {
"first-name":"liu",
"last-name":"yangzuo"
}

var another2_stooge = stooge;
another2_stooge["first-name"] = "zhang";
stooge["first-name"]; // "zhang"
another2_stooge["first-name"]; // "zhang"

接下来我们使用原型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var stooge = {
"first-name":"liu",
"last-name":"yangzuo"
}

Object.create = function(o){
var F = function(){
};
F.prototype = o;
return new F();
}
var another1_stooge = Object.create(stooge);
another1_stooge["first-name"] = "zhang";
stooge["first-name"]; // "liu"
another2_stooge["first-name"]; // "zhang"