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

每个对象都继承原型对象的所有属性

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

猜你喜欢

转载自www.cnblogs.com/adhehe/p/9786772.html