js中用function创建对象时this所创建的属性和方法是对象私有的,也就是所不同的对象拥有不同的拷贝,而prototype创建的属性和方法则是对象公有的,也就是所不同的对象拥都指向同一份拷贝。
function person(name){
this.name = name
this.sayName = function(){
console.log(dd);
}
}
person.prototype.age = 20;
person.prototype.sayAge = function(){
console.log(this.age);
}
var personObj1= new person('ly');
var personObj2 = new person('ly');
console.log(personObj1.sayName == personObj2.sayName); //false
console.log(personObj1.sayAge == personObj2.sayAge); //true