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
相關文章
相關標籤/搜索