Prototype和Object.create继承之间的区别?

时间:2017-09-09 21:33:33

标签: javascript oop prototypal-inheritance

需要你的帮助来了解Prototype和Object.create Inheritance之间的区别是什么? 请描述一下 为什么console.log(objBird.a()); 给出错误。

参考:https://jsfiddle.net/_private/4bmnctoz/

代码:

(function(){
  console.clear();
  var Living = function() {
    var livingCell = 'I am living !';
    this.isLiving = function() {
      return livingCell;
    }
  };
  Living.prototype.a = function() {
    return 'here !!';  
  };
  var objLiving = new Living();
    console.log(objLiving.isLiving());


  var Animal = function() {
    var data = 'I am animal !';
    this.express = function() {
      return data;
    };
  };
  Animal.prototype = new Living();
  Animal.prototype.constructor = Animal;
  var objAnimal = new Animal();
  console.log(objAnimal.express());
  console.log(objAnimal.a());
  console.log(objAnimal.isLiving());

  var Bird = function() {
     var data = 'I am bird !';
    this.express = function() {
      return data;
    };
  };
  var objBird = new Bird();
  objBird.prototype = Object.create(Living);
  objBird.prototype.constructor = Bird;
  console.log(objBird.express());
  console.dir(objBird);
  console.log(objBird.a());
})();

0 个答案:

没有答案