Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's this -- いまさら聞けない JavaScript の this

What's this -- いまさら聞けない JavaScript の this

JavaScript の this は関数定義時に決まるのではなく、関数の呼び出し方によって変わることを解説します。

Shuhei Kagawa

October 31, 2014
Tweet

More Decks by Shuhei Kagawa

Other Decks in Programming

Transcript

  1. ͜͏͍͏ίʔυʹݟ֮͑͸͋Γ·ͤΜ͔ʁ var header = { init: function(selector) { this.el =

    $(selector); this.el.find('.show-modal').on('click', function(e) { this.showModal(); }); this.el.find('.hide-modal').on('click', function(e) { this.hideModal(); }); }, showModal: function() { /* Show modal */ }, hideModal: function() { /* Hide modal */ } }; header.init('.header');
  2. ͜ͷίʔυ͸ಈ͖·ͤΜ var header = { init: function(selector) { this.el =

    $(selector); this.el.find('.show-modal').on('click', function(e) { this.showModal(); }); this.el.find('.hide-modal').on('click', function(e) { this.hideModal(); }); }, showModal: function() { /* Show modal */ }, hideModal: function() { /* Hide modal */ } }; header.init('.header');
  3. ؔ਺ݺͼग़͠ͷύλʔϯ 1. ϝιουݺͼग़͠: obj.func() 2. ؔ਺ݺͼग़͠: func() 3. ίϯετϥΫλݺͼग़͠: new

    Func() 4. call, apply: func.call(obj), func.apply(obj) 5. ଞͷਓʹ࣮ߦΛ೚ͤΔ: ΠϕϯτϦεφʔͳͲ
  4. 1. ϝιουݺͼग़͠ • obj.func() ͱ͢Δͱɺthis ͸ obj ʹͳΔɻ var world

    = { name: ‘World’, greet: function(greeting) { console.log(greeting + ‘, ‘ + this.name); } }; world.greet(‘Hello’); // Hello, World!
  5. 2. ؔ਺ݺͼग़͠ function greet(greeting) { console.log(greeting + ‘, ‘ +

    this.name); } greet(‘Hello’); // Hello, undefined var name = ‘Window’; greet(‘Hello’); // Hello, Window • func() ͱ͢Δͱɺthis ͸άϩʔόϧΦϒδΣΫτʢϒϥ΢βͰ ͸ windowʣʹͳΔɻ
  6. ‘use strict’; Λ࢖͓͏ function world() { 'use strict'; console.log('Hello, '

    + this.name); } world(); // TypeError: Cannot read property 'name' of undefined • ͨͩ͠ strict mode ͩͱ undefined ʹͳΓɺΤϥʔʹؾ ͖ͮ΍͍͢ɻ
  7. 3. ίϯετϥΫλݺͼग़͠ function Person(name, age) { this.name = name; this.age

    = age; } var shuhei = new Person('shuhei', 32); console.log(shuhei.name, shuhei.age); // shuhei 32 • new func() ͷܗࣜͰݺͼग़͢ͱɺthis ͸ίϯελϥΫλ ͕ฦ͢ΠϯελϯεʹͳΔɻ
  8. 4. call, apply function greet(greeting) { console.log(greeting + ', '

    + this.name); } greet.call({ name: 'call' }, ‘Hi'); // Hi, call greet.apply({ name: 'apply' }, [‘Hello']); // Hello, apply • this Λࢦఆͯؔ͠਺Λ࣮ߦͰ͖Δɻ • call ͸ॱ൪ʹҾ਺Λࢦఆ͢Δɻapply ͸Ҿ਺Λ഑ྻͰ౉ͤΔɻ
  9. 5. ଞͷਓʹ࣮ߦΛ·͔ͤΔ var header = { init: function(selector) { this.el

    = $(selector); this.el.find('.show-button').on('click', function(e) { this.showModal(); }); this.el.find('.hide-button').on('click', function(e) { this.hideModal(); }); }, showModal: function() { /* Show modal */ }, hideModal: function() { /* Hide modal */ } }; header.init('.header'); • ଞͷਓ࣍ୈɾɾɾ͓ͦΒ͘ 2 ͔ 4ɻ2ʢάϩʔόϧʣͩͱେมʂ • DOM ͷΠϕϯτϦεφʔͷ৔߹ɺthis ͸ΠϕϯτΛൃՐͨ͠ DOM ཁૉɻ
  10. ରࡦ this.el.find(‘.show-button’).on('click', this.showModal.bind(this)); this.el.find(‘.hide-button').on('click', this.hideModal.bind(this)); • ม਺ʹ this ΛೖΕ͓͍ͯͯɺม਺Λ࢖͏ɻself, that,

    _this ͳͲ͕Α͘࢖ΘΕΔɻ var self = this; this.el.find('.show-button').on('click', function(e) { self.showModal(); }); this.el.find('.hide-button').on('click', function(e) { self.hideModal(); }); • Function.prototype.bind Ͱ this Λࢦఆͨؔ͠਺Λ࡞ΔɻIE9 Ҏ߱Ͱ࢖͑Δ͕ PhantomJS Ͱ࢖͑ͳ͍ͷͨΊɺCI Ͱཁ஫ҙɻes5-shim ͳͲΛೖΕΕ͹࢖͑ΔΑ͏ʹͳΔɻ
  11. ·ͱΊ 1. ϝιουݺͼग़͠: obj.func() -> obj 2. ؔ਺ݺͼग़͠: func() ->

    άϩʔόϧΦϒδΣΫτʢwindow) • strict mode ͳΒ undefined 3. ίϯετϥΫλݺͼग़͠: new Func() -> Πϯελϯε 4. call, apply: func.call(obj), func.apply(obj) -> obj 5. ଞͷਓʹ࣮ߦΛ೚ͤΔ: ΠϕϯτϦεφʔͳͲ -> ଞͷਓͷ࣮૷࣍ୈ