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
360
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
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
34
15k
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
160
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
140
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
370
Domain-Driven Transformation
hschwentner
2
2.3k
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
110
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
7.2k
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
120
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
280
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
330
contenteditable と日本語入力に向き合う
colorful12
0
150
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
210
How STYLIGHT went responsive
nonsquared
100
6.3k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
300
Git: the NoSQL Database
bkeepers
PRO
433
67k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
A better future with KSS
kneath
240
18k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Google's AI Overviews - The New Search
badams
0
1.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
We Are The Robots
honzajavorek
0
390
Making Projects Easy
brettharned
120
6.8k
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); });
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ