js-对象原型
1.什么是原型
我理解的是:就像java中的继承,子类要继承父类中的一些方法。而javascript也如此,在自己没有该属性时,使用父类的属性,自己有属性时则使用自己的属性。
2.使用prototype函数查看原型
利用 prototype 给原型constructor添加公共属性
3.利用 __proto__ 查看对象原型
4利用 in 检验该对象是否有该属性
5.利用 hasOwnProperty 检验该属性是否是对象本生的。
给函数原型添加属性 toString 后
function Person(name , year , gender){
this.name = name;
this.year = year;
this.gender = gender;
}
Person.prototype.toString = function(){
alert("此人信息为:姓名:"+this.name+"年龄:"+this.year+"性别:"+this.gender);
}
var pe1 = new Person("hailing",20,"女");
var pe2 = new Person("yanzi",20,"女");
// pe1.toString();
console.log(pe1.hasOwnProperty("toString"));
console.log(pe1.__proto__.hasOwnProperty("toString"));
console.log(pe1.__proto__.__proto__.hasOwnProperty("toString"));