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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
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
720
Ray.Di / Ray.Aop コトハジメ
madapaja
3
2.5k
Other Decks in Programming
See All in Programming
Vue Fes Japan の “作る側”に関わってみて
naokihaba
1
200
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
12
10k
Modernizing iOS Apps
seyfoyun
0
130
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
11
7k
Event is what happens
moro
2
1.7k
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
190
学びを記事・スライド・Xへ高速展開する― AI-assisted Learning Loopで回すコンテンツ生成術
ochtum
0
150
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
210
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
160
dotNET_vulnclicker_intro.pdf
deka_morita
0
150
パスワード再設定メールのURLについて知っておこう
uyuki234
0
190
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
280
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
540
We Have a Design System, Now What?
morganepeng
55
8.4k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
560
エンジニアに許された特別な時間の終わり
watany
109
250k
Mind Mapping
helmedeiros
1
410
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
330
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
300
Music & Morning Musume
bryan
48
7.5k
Transcript
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); });
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ