编辑代码

function Father(name, age) {
      this.name = name;
      this.age = age;
    }
function Son(name, age) {
    Father.call(this, name, age);
}
Father.prototype.money = function() {
    console.log('I have 100000$!');
}//在Father的原型对象上创建一个新的方法,这个方法才会被继承
Son.prototype = new Father();
Son.prototype.constructor = Son;
Son.prototype.habbit = function() {
    console.log('I like playing!');
}//在Son的原型对象上创建一个新的方法,这个方法才会被继承

var grandSon = function(name,age,height){
    Son.call(this,name,age)
    this.height = height
}


grandSon.prototype = new Son()//new 会调用构造函数Son的原型对象创建一个新的Son对象,返回这个原型对象给grandSon的原型
grandSon.prototype.constructor = grandSon//让grandSon的原型对象所指向的构造函数是grandSon
grandSon.prototype.eat = () =>{
    console.log("eat")
    console.log(this)
}

var son = new Son('mmm', 17);
var father = new Father('cnm',123)
var grandson = new grandSon('cs',3,29)


console.log(father.__proto__.constructor)
father.money()


console.log(son.__proto__);
son.money();  // I have 100000$! son调用father的方法
son.habbit(); // I like playing!  son调用自己的方法

//原型链
console.log(grandson.__proto__)//一个实例化的son对象,带有eat方法
console.log(grandson.__proto__.__proto__)//一个实例化的father对象,带有habbit方法
console.log(grandson.__proto__.__proto__.__proto__)//一个实例化的father对象,带有money方法
console.log(grandson.__proto__.__proto__.__proto__.__proto__)//object对象

grandson.eat()
grandson.habbit()
grandson.money()


/////对象关联(行为委托)

foo = {
  init: function(name,label) {
    this.name = name;
    this.label = label;
  },
  myName: function() {
    return "I am " + this.name;
  }
};
Bar = Object.create( foo );
Bar.myLabel = function(){
  return this.label;
};
var b1 = Object.create( Bar );
b1.init("a","obj a");
b1.myName();
b1.myLabel();