<script>
function Person(name, age) {
this.name = name;
this.age = age;
this.family = ["father" , "mather" , "sister"] ;
}
Person.prototype = {
constructor: Person,
say : function () {
return "name : " + this.name + "age: " + this.age;
}
} ;
var p1 = new Person("zhangsan", 19);
console.log(p1.family);
p1.family.push("brother") ;
console.log(p1.family) ;
var p2 = new Person("lisi" , 20) ;
console.log(p2.family) ;
</script>
输出:
["father", "mather", "sister"]
["father", "mather", "sister", "brother"]
["father", "mather", "sister"]
注意,数组是引用类型,如果把数组放在原型中,对p1的family的改动会修改p2。所以将引用类型放在构造函数中。