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

JS/jQueryのはなし

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 JS/jQueryのはなし

じげん制作勉強会での資料です。

Avatar for じげん公式

じげん公式

October 31, 2014
Tweet

More Decks by じげん公式

Other Decks in Programming

Transcript

  1. είʔϓ var greeting = 'Hello, '; function greet(name) { var

    message = greeting + name; isGreeted = true; alert(message); } greet('World'); // ← ? console.log(message); // ← ? console.log(isGreeted); // ← ?
  2. είʔϓ 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
  3. άϩʔόϧม਺ͱϩʔΧϧม਺ var greeting = 'Hello, '; // ←άϩʔόϧม਺ function greet(name)

    { var message = greeting + name; // ←ϩʔΧϧม਺ isGreeted = true; // ←άϩʔόϧม਺ alert(message); }
  4. ΋͏Ұ౓ݟͯΈΑ͏ var greeting = 'Hello, '; function greet(name) { //

    ← functionͷதͰ… var message = greeting + name; // ← var Λ༻͍ͯม਺એݴʂ isGreeted = true; alert(message); }
  5. Α͘࢖͏ςΫχοΫ ΋ͪΖΜjQueryΛ࢖ͬͨҎԼͷΑ͏ͳॻ͖ํͰ΋OK $.ready(function(){ // ← functionͷதͰ… var hoge; // ←

    var એݴ 㱺 ϩʔΧϧม਺ʂ }); $(function(){ // ← functionͷதͰ… var hoge; // ← var એݴ 㱺 ϩʔΧϧม਺ʂ });
  6. jQuery Ͱͷ this ͷྫ $('a').on('click', function() { var link_to =

    $(this).attr('href'); return window.confirm( link_to + '΁Ҡಈ͠·͔͢ʁ' ) }); • ͜ͷ this ͸ΫϦοΫ͞Εͨ a λάࣗ਎Λࢦ͍ͯ͠Δ
  7. ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this var user = { name: 'ଠ࿠', greet: function()

    { alert('Hello, ' + this.name + '!'); } }; user.greet(); // 'Hello, ଠ࿠!' ͱΞϥʔτ͞ΕΔ
  8. ؔ਺Λ new ͨ࣌͠ͷ this function User(name) { this.name = name;

    } var user = new User('ଠ࿠'); alert(user.name); // 'ଠ࿠' ͱΞϥʔτ͞ΕΔ
  9. .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, Ֆࢠ!' ͱΞϥʔτ͞ΕΔ
  10. ͦΕҎ֎ͷ this function setOnClick() { this.onclick = function() { alert('Click͞Ε·ͨ͠!');

    }; } setOnClick(); • ͜ͷ this ͸άϩʔόϧΦϒδΣΫτΛࢦ͍ͯ͠Δ • ϒϥ΢βͷάϩʔόϧΦϒδΣΫτ͸ window ΦϒδΣΫτ • this.onclick ͸ window.onclick ͱಉ͡ҙຯ
  11. ͜ͷ this ͸Կʁ var obj = { hoge: function() {

    this; // →ʁ } }; obj.hoge();
  12. ౴͑ $("li").each(function() { this; // → li ΦϒδΣΫτ }); jQuery಺Ͱͷ࣮૷Πϝʔδ

    each: function(callback) { for (var i = 0; i < elements.length; i++) { callback.call(elements[i]); // ← } }
  13. ͜ͷ৔߹ $('img').hide().on('load', function() { setTimeout(function() { $(this).fadeIn(); }, 1000); });

    • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this • ؔ਺Λ new ͨ࣌͠ͷ this • .call()ɺ.apply() ΍ .bind() ͷࡍͷ this • ͦΕҎ֎ͷ this
  14. ෮श͠·͢ • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this → ͦͷΦϒδΣΫτ ࣗ਎ • ؔ਺Λ new

    ͨ࣌͠ͷ this → new͞ΕͨΦϒδΣΫτࣗ਎ • .call()ɺ.apply() ΍ .bind() ͷࡍͷ this → Ҿ਺Ͱࢦఆ ͨ͠ΦϒδΣΫτ • ͦΕҎ֎ͷ this → άϩʔόϧΦϒδΣΫτʢwindowʣ
  15. ͜͏͢Ε͹OK $('img').hide().on('load', function() { var $img = $(this); // ←

    ม਺ʹ୅ೖͯ͠ setTimeout(function() { $img.fadeIn(); // ← ࢖͏ }, 1000); });
  16. ·ͱΊ this ͳΜͯා͘ͳ͍ • ΦϒδΣΫτϝιουݺͼग़࣌͠ͷ this → ͦͷΦϒδΣΫτ ࣗ਎ •

    ؔ਺Λ new ͨ࣌͠ͷ this → new ͞ΕͨΦϒδΣΫτࣗ਎ • .call()ɺ.apply() ΍ .bind() ͷࡍͷ this → Ҿ਺Ͱࢦఆ ͨ͠ΦϒδΣΫτ • ͦΕҎ֎ͷ this → άϩʔόϧΦϒδΣΫτʢwindowʣ
  17. ·ͱΊ this ͳΜͯා͘ͳ͍ $('img').hide().on('load', function() { var $img = $(this);

    // ←ม਺ʹ୅ೖͯ͠͏·͘෇͖߹͓͏ setTimeout(function() { $img.fadeIn(); }, 1000); });