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

JS/jQueryのはなし | リジョブ社内勉強会

JS/jQueryのはなし | リジョブ社内勉強会

madapaja

May 12, 2015
Tweet

More Decks by madapaja

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 → άϩʔόϧΦϒδΣΫτ
  17. ·ͱΊ this ͳΜͯා͘ͳ͍ $('img').hide().on('load', function() { var $img = $(this);

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