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

Javascript 2016 with ES6

Nokogiri
December 15, 2016

Javascript 2016 with ES6

社内向け勉強会資料

Nokogiri

December 15, 2016
Tweet

More Decks by Nokogiri

Other Decks in Programming

Transcript

  1. 1.class class Member { // ίϯετϥΫλʔ͸private constructor(name, age) { this.name

    = name; this.age = age; } // ϝιου͸͢΂ͯpublic getInfo() { return `I’m ${this.name}. ${this.age} year's old.`; } } //Πϯελϯεੜ੒ var tom = new Member(‘Tom’, 30); //ϝιου࣮ߦ console.log(tom.getInfo()); //-> I’m Tom. 30 year's old.
  2. 1.class class Member { // ίϯετϥΫλʔ͸private constructor(name, age) { this.name

    = name; this.age = age; } // ϝιου͸͢΂ͯpublic getInfo() { return `I’m ${this.name}. ${this.age} year's old.`; } } //Πϯελϯεੜ੒ var tom = new Member(‘Tom’, 30); //ϝιου࣮ߦ console.log(tom.getInfo()); //-> I’m Tom. 30 year's old. ҎԼͷػೳ΋ར༻Մೳ w ܧঝ w ήολʔηολʔ HFUTFU  w TUBUJDϝιου
  3. 3.ؔ਺ߏจͷվળ var calcTriangleArea = function(base, height) { // ࡾ֯ܕͷ໘ੵΛܭࢉͯ͠ฦ٫ return

    base * height / 2 //ఈล × ߴ͞ ÷ 2 } console.log(calcTriangleArea(5, 4) + ‘cᶷ’); //-> 10cᶷ 3-1.Ξϩʔؔ਺ طଘ var calcTriangleArea = (base, height) => { // ࡾ֯ܕͷ໘ੵΛܭࢉͯ͠ฦ٫ return base * height / 2 //ఈล × ߴ͞ ÷ 2 } console.log(calcTriangleArea(5, 4) + ‘cᶷ’); //-> 10cᶷ Ξϩʔؔ਺
  4. 3.ؔ਺ߏจͷվળ // ԁͷ໘ੵΛٻΊΔ var calcCircleArea = radius => radius *

    radius * Math.PI; console.log(calcCircleArea(5) + ‘cᶷ’); //-> 78.53981633974483cᶷ 3-1.Ξϩʔؔ਺ var show = () => console.log(‘ݩؾͰ͔͢ʁ’); show(); //-> ݩؾͰ͔͢ʁ Ξϩʔؔ਺(Ҿ਺ͳ͠) Ξϩʔؔ਺(Ҿ਺1ͭ & 1ߦ)
  5. 3.ؔ਺ߏจͷվળ var calcTriangleArea = function(base = 5, height = 4)

    { // ࡾ֯ܕͷ໘ੵΛܭࢉͯ͠ฦ٫ return base * height / 2 //ఈล × ߴ͞ ÷ 2 }; console.log(calcTriangleArea() + ‘cᶷ’); //-> 10cᶷ console.log(calcTriangleArea(6) + ‘cᶷ’); //->12cᶷ console.log(calcTriangleArea(,6) + ‘cᶷ’); //-> Τϥʔ 3-2.Ҿ਺ͷσϑΥϧτ஋
  6. 3.ؔ਺ߏจͷվળ //Ҿ਺Λશͯ߹ࢉͯ͠ฦ٫͢Δ var calcSum = (...nums) => { let sum

    = 0; for (let n of nums) { sum += n; } return sum; }; console.log(calcSum(1,2,3,4,5,6)); //-> 21 3-3.Մม௕Ҿ਺ ྫ1)
  7. 4.let / const ͷείʔϓ var scope = “global_scope"; var getValue

    = function() { var scope = ‘local_scope’; return scope; }; if (true) { var new_scope1 = ‘new_scope1’; } for (var i = 0,len = 3; i < len; i++ ) { var new_scope2 = 'new_scope2'; }; console.log(getValue()); // -> local_scope console.log(scope); // -> global_scope console.log(new_scope1); // -> new_scope1 console.log(new_scope2); // -> new_scope2 طଘͷείʔϓ
  8. 4.let / const ͷείʔϓ var scope = “global_scope"; var getValue

    = function() { var scope = ‘local_scope’; return scope; }; if (true) { var new_scope1 = ‘new_scope1’; } for (var i = 0,len = 3; i < len; i++ ) { var new_scope2 = 'new_scope2'; }; console.log(getValue()); // -> local_scope console.log(scope); // -> global_scope console.log(new_scope1); // -> new_scope1 console.log(new_scope2); // -> new_scope2 طଘͷείʔϓ WBSͳ͠ͷએݴ͸ શͯHMPCBMʹͳΔͷͰඇਪ঑ ΤϥʔͱͳΒͳ͍
  9. let scope = ‘aaa’; let scope = ‘bbb’; // Τϥʔ

    4.let / const ͷείʔϓ if (true) { let new_scope = ‘new_scope’; } console.log(new_scope); // Τϥʔ let ྫ̍) let ྫ2) const scope = ‘aaa’; scope = ‘bbb’; // Τϥʔ const ྫ1)
  10. let scope = ‘aaa’; let scope = ‘bbb’; // Τϥʔ

    4.let / const ͷείʔϓ if (true) { let new_scope = ‘new_scope’; } console.log(new_scope); // Τϥʔ let ྫ̍) let ྫ2) ϒϩοΫ֎͸Τϥʔ એݴͷॏෳ͸Τϥʔ const scope = ‘aaa’; scope = ‘bbb’; // Τϥʔ const ྫ1) ஋ͷ࠶୅ೖෆՄ +BWBͰ͍͏pOBM جຊ͸const ࠶୅ೖ͕ඞཁʹͳͬͨΒletΛར༻(var͸ඇਪ঑) airbnbϧʔϧ
  11. 5. Map / Set let m = new Map(); m.set(‘javascript’:’express’);

    m.set(‘ruby’:’ruby on rails’); m.set(‘python’:’Djungo’); console.log(m.size); //->3 console.log(m.get(‘ruby’)); //->ruby on rails console.log(m.has(‘ruby’)); //->true Map let s = new Set(); s.add(100); s.add(50); s.add(100); for (let item of s) { console.log(item); //->100,50 } Set
  12. 5. Map / Set let m = new Map(); m.set(‘javascript’:’express’);

    m.set(‘ruby’:’ruby on rails’); m.set(‘python’:’Djungo’); console.log(m.size); //->3 console.log(m.get(‘ruby’)); //->ruby on rails console.log(m.has(‘ruby’)); //->true Map let s = new Set(); s.add(100); s.add(50); s.add(100); //->100͸ॏෳ for (let item of s) { console.log(item); //->100,50 } Set +BWBͷNBQͱಉ͡ ஋ͷॏෳΛڐ͞ͳ͍
  13. 6. for … of(ΠςϨʔλʔ) var numberlist = [1,2,3,4]; for (var

    i = 0, len = numberlist.length; i < len; i++) { console.log(i); //->1,2,3,4 } for const numberlist = [1,2,3,4]; for (const item of numberlist) console.log(item); //->1,2,3,4 } let data_str = “͍͋͏͓͑"; for(let item of data_str) { console.log(item);//->͋ ͍ɹ͏ ͑ ͓ } for of
  14. 7. δΣωϨʔλʔ let showlog = function* () { // functionͷ్தͰԿճͰ΋ϦλʔϯͰ͖Δ

    yield '͸͡Ίͯͷ'; yield 'δΣωϨʔλʔ'; yield 'ɻ'; }; // δΣωϨʔλʔ͸ྻڍՄೳͳΦϒδΣΫτΛฦ٫͢Δ for (let item of showlog()) { console.log(item); } δΣωϨʔλʔ