Показать сообщение отдельно
  #1 (permalink)  
Старый 26.01.2017, 15:30
Профессор
Отправить личное сообщение для Mess4me Посмотреть профиль Найти все сообщения от Mess4me
 
Регистрация: 03.11.2014
Сообщений: 263

Метод Object.create
Добрый день!
Есть скрипт
function Animal(name){
        console.log('Animal',this);
        this.name = name;
    }

    Animal.prototype.getName = function () {
        console.log('getName',this);
        return this.name;
    };

    function Cat(name){
        console.log('Cat',this);
        Animal.call(this, name);
    }
    Cat.prototype = Object.create(Animal.prototype);
    Cat.prototype.meow = function(){
        console.log('meow',this);
        return 'Cat '+ this.getName() +' is saying meow';
    };



    var cat = new Cat('garfield');
    console.log(cat.getName() === 'garfield'); // true
    console.log(cat.meow() === 'Cat garfield is saying meow'); // true


Чем чем будет отличаться логига , если строку 15 заменить на:
Cat.prototype = Animal.prototype;


Если посмотреть на эмуляцию метода Object.create он возвращает объект :

function inherit(proto) {
  function F() {}
  F.prototype = proto;
  var object = new F;
  return object;
}
Ответить с цитированием