JavaScript基础概念之----继承

每一个对象都继承原型对象的全部属性this

function People(name,age){
    this.name = name;
    this.age = age;
}

People.prototype.getWolk = function(){
    console.log('getWolk');
}

var o1 = new People('adhehe',23);
var o2 = new People('Jhone',46);

console.log(o1.name)//输出adhehe
console.log(o2.name)//输出Jhone o1.getWolk()
//输出getWolk o2.getWolk() //输出getWolk function Child(){} Child.prototype = new People(); var o3 = new Child('Halun',2); var o4 = new Child('Plili',3);
console.log(o3.name)//输出undefined
console.log(o4.name)//输出undefined o3.getWolk()
//输出getWolk o4.getWolk()//输出getWolk
相关文章
相关标签/搜索