new vs assign

by kenji morita

JavaScript

////newして継承する記述////////////////////////////////////////////////////////
function Animal() {
  this.x = 0;
  this.y = 0;
}
Animal.prototype.locate = function() {
  console.log(this.x, this.y);
  return this;
};
Animal.prototype.move = function(x, y) {
  this.x = this.x + x;
  this.y = this.y + y;
  return this;
}

function Duck() {
  Animal.call(this);
}
Duck.prototype = new Animal();
Duck.prototype.constructor = Duck;
Duck.prototype.speak = function() {
  console.log("quack");
  return this;
}
var daffy = new Duck();
console.log(daffy.move(6, 7).locate().speak());



////assignで継承する記述////////////////////////////////////////////////////////
var animal = {
  x: 0,
  y: 0,
  locate: function() {
    console.log(this.x, this.y);
    return this;
  },
  move: function(x, y) {
    this.x = this.x + x;
    this.y = this.y + y;
    return this;
  }
}
var duck = function() {
  return Object.assign(Object.create(animal), {
    speak: function() {
      console.log("quack");
      return this;
    }
  });
}
var daffy = duck();
console.log(daffy.move(6, 7).locate().speak());

//http://stackoverflow.com/questions/33692912/using-object-assign-and-object-create-for-inheritance