Slide 1

Slide 1 text

What’s “this”? ͍·͞Βฉ͚ͳ͍ JavaScript ͷ this @M3 Tech Talk 2014/10/31 ߳઒ पฏ

Slide 2

Slide 2 text

͜͏͍͏ίʔυʹݟ֮͑͸͋Γ·ͤΜ͔ʁ 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');

Slide 3

Slide 3 text

͜ͷίʔυ͸ಈ͖·ͤΜ 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');

Slide 4

Slide 4 text

this ʹԿ͕ೖͬͯ͘Δ͔ Ͳ͏΍ܾͬͯ·ΔͰ͠ΐ͏ʁ 1. this ΛแΉؔ਺ͷఆٛ࣌ͷείʔϓͰܾ·Δɻ 2. this ΛแΉؔ਺ͷఆٛͷ࢓ํͰܾ·Δɻ 3. this ΛแΉؔ਺ͷ࣮ߦ࣌ͷείʔϓͰܾ·Δɻ 4. this ΛแΉؔ਺ͷ࣮ߦͷ࢓ํͰܾ·Δɻ

Slide 5

Slide 5 text

this ʹԿ͕ೖͬͯ͘Δ͔ Ͳ͏΍ܾͬͯ·ΔͰ͠ΐ͏ʁ 1. this ΛแΉؔ਺ͷఆٛ࣌ͷείʔϓͰܾ·Δɻ 2. this ΛแΉؔ਺ͷఆٛͷ࢓ํͰܾ·Δɻ 3. this ΛแΉؔ਺ͷ࣮ߦ࣌ͷείʔϓͰܾ·Δɻ 4. this ΛแΉؔ਺ͷ࣮ߦͷ࢓ํͰܾ·Δɻ

Slide 6

Slide 6 text

this ͸ 0 ݸ໨ͷҾ਺ • this ͸ɺؔ਺ͷఆٛ࣌Ͱ͸ͳ͘ɺؔ਺ͷݺͼग़͠ํ ʹΑ࣮ͬͯߦ࣌ʹܾ·Δɻ

Slide 7

Slide 7 text

ؔ਺ݺͼग़͠ͷύλʔϯ 1. ϝιουݺͼग़͠: obj.func() 2. ؔ਺ݺͼग़͠: func() 3. ίϯετϥΫλݺͼग़͠: new Func() 4. call, apply: func.call(obj), func.apply(obj) 5. ଞͷਓʹ࣮ߦΛ೚ͤΔ: ΠϕϯτϦεφʔͳͲ

Slide 8

Slide 8 text

1. ϝιουݺͼग़͠ • obj.func() ͱ͢Δͱɺthis ͸ obj ʹͳΔɻ var world = { name: ‘World’, greet: function(greeting) { console.log(greeting + ‘, ‘ + this.name); } }; world.greet(‘Hello’); // Hello, World!

Slide 9

Slide 9 text

2. ؔ਺ݺͼग़͠ function greet(greeting) { console.log(greeting + ‘, ‘ + this.name); } greet(‘Hello’); // Hello, undefined var name = ‘Window’; greet(‘Hello’); // Hello, Window • func() ͱ͢Δͱɺthis ͸άϩʔόϧΦϒδΣΫτʢϒϥ΢βͰ ͸ windowʣʹͳΔɻ

Slide 10

Slide 10 text

‘use strict’; Λ࢖͓͏ function world() { 'use strict'; console.log('Hello, ' + this.name); } world(); // TypeError: Cannot read property 'name' of undefined • ͨͩ͠ strict mode ͩͱ undefined ʹͳΓɺΤϥʔʹؾ ͖ͮ΍͍͢ɻ

Slide 11

Slide 11 text

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 ͸ίϯελϥΫλ ͕ฦ͢ΠϯελϯεʹͳΔɻ

Slide 12

Slide 12 text

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 ͸Ҿ਺Λ഑ྻͰ౉ͤΔɻ

Slide 13

Slide 13 text

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 ཁૉɻ

Slide 14

Slide 14 text

ରࡦ 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 ͳͲΛೖΕΕ͹࢖͑ΔΑ͏ʹͳΔɻ

Slide 15

Slide 15 text

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