function Person(){} Person.prototype = { name:"name1", friends:["f1","f2"], sayName:function(){ alert(this.name); } };
var p1 = new Person();
var p2 = new Person();
p1.friends.push("f3");
alert(p1.friends); //f1,f2,f3
alert(p2.friends);//f1,f2,f3
alert(p1.friends===p2.friends); //true
Person.protoype 对象有一个名为firends的属性,该属性包含一个字符串数组。然后,创建了Person的两个实例。接着修改了person1.friends引用的数组,向数组中添加了一个字符串。由于firends数组存在于Person.prototype而非p1中,所以刚刚提到的修改会通过p2.friends(与p1.friends 指向同一个数组)反映出来。