Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
JS/jQueryのはなし | リジョブ社内勉強会
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
madapaja
May 12, 2015
Programming
370
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JS/jQueryのはなし | リジョブ社内勉強会
madapaja
May 12, 2015
Transcript
46 slides
JS/jQueryͷ @Ϧδϣϒࣾษڧձ 2015-5-12
JS/jQueryΛ ͍͜ͳͨ͢Ίʹ େͳ͜ͱΛ ͠·͢
ࠓ͓͢͠Δ͜ͱ • JavaScriptͷείʔϓͷ͜ͱ • JavaScriptͷ this ͷ͜ͱ • ·ͱΊ
είʔϓͱԿ͔ʁ
είʔϓͱ ม͕ఆ໊ٛͨ͠લͰ͑Δ ൣғͷ͜ͱ
είʔϓͱ JavaScriptͰେ͖͘ҎԼͷ2ͭʹ͚ΒΕΔ • άϩʔόϧείʔϓ Ͳ͔͜ΒͰࢀরͰ͖Δείʔϓ • ϩʔΧϧείʔϓ ಛఆͷൣғͰࢀরͰ͖Δείʔϓ ͔͜͜ΒɺมͷείʔϓΛݟ͍͖ͯ·͢ɻ
είʔϓ var greeting = 'Hello, '; function greet(name) { var
message = greeting + name; isGreeted = true; alert(message); } greet('World'); // ← ? console.log(message); // ← ? console.log(isGreeted); // ← ?
είʔϓ var greeting = 'Hello, '; function greet(name) { var
message = greeting + name; isGreeted = true; alert(message); } greet('World'); // ← 'Hello, World' ͱΞϥʔτ console.log(message); // ← ະఆٛΤϥʔ console.log(isGreeted); // ← true
άϩʔόϧมͱϩʔΧϧม var greeting = 'Hello, '; // ←άϩʔόϧม function
greet(name) { var message = greeting + name; // ←ϩʔΧϧม isGreeted = true; // ←άϩʔόϧม alert(message); }
ϩʔΧϧมΛ࡞Δʹ 1. function ͷதͰมએݴ͢Δ 2. var Λ༻͍ͯมએݴ͢Δ ͷ྆ํ͕ඞͣඞཁ
͏ҰݟͯΈΑ͏ var greeting = 'Hello, '; function greet(name) { //
← functionͷதͰ… var message = greeting + name; // ← var Λ༻͍ͯมએݴʂ isGreeted = true; alert(message); }
Α͘͏ςΫχοΫ ଈ࣌ؔͰғ͍είʔϓΛ࡞Δ (function(){ // ← functionͷதͰ… var hoge; // ←
var એݴ 㱺 ϩʔΧϧมʂ })();
Α͘͏ςΫχοΫ ͪΖΜjQueryΛͬͨҎԼͷΑ͏ͳॻ͖ํͰ OK $.ready(function(){ // ← functionͷதͰ… var hoge; //
← var એݴ 㱺 ϩʔΧϧมʂ }); $(function(){ // ← functionͷதͰ… var hoge; // ← var એݴ 㱺 ϩʔΧϧมʂ });
είʔϓͷޮՌ มͷিಥ͕ආ͚Δ ಉ͡ม໊͕͋ͬͯɺผͷείʔϓͰ͋Ε ผͷมͱͳΔͨΊɺؒҧͬͯ༻தͷมΛ ॻ͖͑ͯ͠·ͬͨΓͱ͍ͬͨࣄނΛ͍͗͢
άϩʔόϧมΘͳ͍!!! • άϩʔόϧม୭ͰಡΈॻ͖ग़དྷͪΌ͏Α • ͔͍ࣗͬͯ͠ͳ͍ͱࢥ͍ͬͯͨάϩʔόϧ ม ผͷਓͬͯΔ͔͠Εͳ͍
άϩʔόϧมΘͳ͍!!! • ͦͷάϩʔόϧมͰɺผͷεΫϦϓτͷڍಈ ʹӨڹ͢Δ͔ • ͦͷάϩʔόϧมɺผͷεΫϦϓτ͔Β ॻ͖͑ΒΕΔ͔ • ͦͷϖʔδʹ͍ΖΜͳਓ͕ॻ͍ͨJS͕ಡΈࠐ ·Ε͍ͯΔ
͔ͩΒ
άϩʔόϧม Θͳ͍!!!
μϝɻθολΠɻά ϩʔόϧมʂ
ʢͪΖΜάϩʔόϧมΛΘͳ͚ΕͳΒͳ ͍έʔε͋ΔͷͰɺͦͷ࣌༻๏ɾ༰ྔΛकͬ ͯ༻͠·͠ΐ͏ʣ
this ͱԿ͔ʁ
this ͱ • JavaScriptͰࣗಈతʹఆٛ͞ΕΔϚδοΫม • Ͳ͜Ͱ this Λ͏͔ʢ͘͠ݺͼग़͞Ε ํʣʹΑΓɺ ࢦ͢༰͕ҟͳΔ
jQuery Ͱͷ this ͷྫ $('a').on('click', function() { var link_to =
$(this).attr('href'); return window.confirm( link_to + 'Ҡಈ͠·͔͢ʁ' ) }); • ͜ͷ this ΫϦοΫ͞Εͨ a λάࣗΛࢦ͠ ͍ͯΔ
ؔͰͷ this 4ύλʔϯΛ͑ΕOK • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this • ؔΛ new ͨ࣌͠ͷ
this • .call()ɺ.apply() .bind() ͷࡍͷ this • ͦΕҎ֎ͷ this
ΦϒδΣΫτϝιουݺͼग़ ࣌͠ͷ this var user = { name: 'ଠ', greet:
function() { alert('Hello, ' + this.name + '!'); } }; user.greet(); // 'Hello, ଠ!' ͱΞϥʔτ͞ΕΔ
ؔΛ new ͨ࣌͠ͷ this function User(name) { this.name = name;
} var user = new User('ଠ'); alert(user.name); // 'ଠ' ͱΞϥʔτ͞ΕΔ
.call()ɺ.apply() .bind() ͷࡍ ͷ this ͍͜͠ͷͰงғؾ͚ͩ var user =
{ name: 'ଠ', greet: function() { alert('Hello, ' + this.name + '!'); } }; user.greet.call({name: 'Ֆࢠ'}); // 'Hello, Ֆࢠ!' ͱΞϥʔτ͞ΕΔ user.greet.apply({name: 'Ֆࢠ'}); // 'Hello, Ֆࢠ!' ͱΞϥʔτ͞ΕΔ var userHanakoGreet = user.greet.bind({name: 'Ֆࢠ'}); userHanakoGreet(); // 'Hello, Ֆࢠ!' ͱΞϥʔτ͞ΕΔ
ͦΕҎ֎ͷ this function setOnClick() { this.onclick = function() { alert('Click͞Ε·ͨ͠!');
}; } setOnClick(); • ͜ͷ this άϩʔόϧΦϒδΣΫτΛࢦͯ͠ ͍Δ • ϒϥβͷάϩʔόϧΦϒδΣΫτ window ΦϒδΣΫτ • this.onclick window.onclick ͱಉ͡ҙຯ
͏ᘳͰ͢Ͷʁ
͜ͷ this Կʁ (function(){ this; // !ʁ });
͑ (function(){ this; // !window(άϩʔόϧΦϒδΣΫτ) });
͜ͷ this Կʁ var obj = { hoge: function() {
this; // !ʁ } }; obj.hoge();
͑ var obj = { hoge: function() { this; //
!obj } }; obj.hoge();
͜ͷ this Կʁ $("li").each(function() { this; // !ʁ });
͑ $("li").each(function() { this; // ! li ΦϒδΣΫτ }); jQueryͰͷ࣮Πϝʔδ
each: function(callback) { for (var i = 0; i < elements.length; i++) { callback.call(elements[i]); // ← } }
ظͨ͠ಈ࡞ʹͳΔʁ // ը૾͕ϩʔυ͞Εͯ $('img').hide().on('load', function() { // 1ඵޙʹϑΣʔυΠϯ setTimeout(function() {
$(this).fadeIn(); }, 1000); });
ظͨ͠ಈ࡞͠ ͳ͍ʂ
ͳͥʁ
͜ͷ߹ $('img').hide().on('load', function() { setTimeout(function() { $(this).fadeIn(); }, 1000); });
• ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this • ؔΛ new ͨ࣌͠ͷ this • .call()ɺ.apply() .bind() ͷࡍͷ this • ͦΕҎ֎ͷ this
෮श͠·͢ • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this → ͦ ͷΦϒδΣΫτࣗ • ؔΛ new
ͨ࣌͠ͷ this → new͞ΕͨΦϒδ ΣΫτࣗ • .call()ɺ.apply() .bind() ͷࡍͷ this → ҾͰࢦఆͨ͠ΦϒδΣΫτ • ͦΕҎ֎ͷ this → άϩʔόϧΦϒδΣΫτ ʢwindowʣ
͜ͷ this window $('img').hide().on('load', function() { setTimeout(function() { $(this).fadeIn();
}, 1000); });
͜͏͢ΕOK $('img').hide().on('load', function() { var $img = $(this); // ←
มʹೖͯ͠ setTimeout(function() { $img.fadeIn(); // ← ͏ }, 1000); });
·ͱΊ
·ͱΊ ϩʔΧϧείʔϓେਓͷͩ͠ͳΈ 1. function ͷதͰ 2. varΛ༻͍ͯมએݴ͠Α͏ (function(){ var i_am_free
= 'ࣗݾදݱʂ'; })();
·ͱΊ this ͳΜͯා͘ͳ͍ • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this → ͦ ͷΦϒδΣΫτࣗ •
ؔΛ new ͨ࣌͠ͷ this → new ͞ΕͨΦϒδ ΣΫτࣗ • .call()ɺ.apply() .bind() ͷࡍͷ this → ҾͰࢦఆͨ͠ΦϒδΣΫτ • ͦΕҎ֎ͷ this → άϩʔόϧΦϒδΣΫτ
·ͱΊ this ͳΜͯා͘ͳ͍ $('img').hide().on('load', function() { var $img = $(this);
// ←มʹೖͯ͠͏·͖͘߹͓͏ setTimeout(function() { $img.fadeIn(); }, 1000); });
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ
More Decks by madapaja
See All by madapaja
BEAR.Sunday のはなし - 第89回PHP勉強会@東京
madapaja
3
2.4k
Ray.Di-Ray.AopからみるPHP5.4+フレームワークBEAR.Sunday
madapaja
3
730
Ray.Di / Ray.Aop コトハジメ
madapaja
3
2.5k
Other Decks in Programming
See All in Programming
[DroidKnights 2026] Workflow AX 적용기 — 자동 정책 정리부터 E2E까지
dayeon
0
120
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
180
小さなアプリを作って公開するまで
onozaty
0
250
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
420
How SRE Grew Out of Server-Side Work and Became the Team's Own / サーバサイドから始まったSREを チームに渡すまで
maaaato
1
230
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
3
220
あなたはMCPの機能を、まだ知らない。
4geru
1
740
どうしても Eloquent でリポジトリパターンをやりたいんだ #phpcon_niigata / phpcon-niigata-2026
shogogg
0
680
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
420
はじめてのPLATEAU
nokonoko1203
1
670
正解の無い問題を 「できるだけ上手く」解く ヒューリスティックで感性を磨こう。〜現実課題をプログラミングで解きあうコミュニティについて〜
allegrogiken
0
110
Featured
See All Featured
Heart Work Chapter 1 - Part 1
lfama
PRO
11
37k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
Code Review Best Practice
trishagee
74
20k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
860
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
3k
From π to Pie charts
rasagy
1
460
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
1k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
121
130k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
My Coaching Mixtape
mlcsv
0
400