Make it to make it

いろいろ作ってアウトプットするブログ

this: new binding, window binding

new binding

/**
 * new Binding
 */
const Animal = function(color, name, type) {
  this.color = color
  this.name = name
  this.type = type
}
const zebra = new Animal('black and white', 'shimauma', 'plant-eating animal')
console.log(zebra)

下記のようにオブジェクトを返却する。

Animal { color: 'black and white', name: 'shimauma', type: 'plant-eating animal' }

window binding

/**
 * window Binding
 */
const sayAge = function() {
  console.log(this.age)
}
const me = {
  age: 25
}
sayAge.call(me)

引数なしで実行した場合

sayAge()

undefinedを返却する

const sayAge = function() {
  'use strict'
  console.log(this.age)
}

strictモードで実行すると、エラーをはく。

Cannot read property 'age' of undefined