ECMAScript2015 (ES2015 / ES6)ã§è¿½å ããããé åããªãã¸ã§ã¯ããåå²ãã¦å¤æ°ã«ä»£å ¥ã§ããåå²ä»£å ¥ã§è²ã ã¨ã©ã¼ã«ãªã£ã¦ããã£ãã®ã§ã¡ã¢ã
åå²ä»£å ¥ Destructuring assignment
分割代入 - JavaScript | MDN ã¨ã¯
// é å let [a, b, ...rest] = [10, 20, 30, 40, 50]; console.log(a); // 10 console.log(b); // 20 console.log(rest); // [30, 40, 50] // ãªãã¸ã§ã¯ã let {cute, cool, sexy, ...other} = { cute: 'æå®®ãã¡ã', cool: 'é§ç¢ããã', pop: 'ææ å·ãã¨ã', sexy: 'ç´«å¹è', legend: 'ç¥åç¾æ' }; console.log(cute); // æå®®ãã¡ã console.log(cool); // é§ç¢ããã console.log(sexy); // ç´«å¹è console.log(other); // { pop: 'ææ å·ãã¨ã', legend: 'ç¥åç¾æ' }
ãããªã®ã
é
åã¯åããé çª(ã¤ã³ããã¯ã¹é )ã«ããªãã¸ã§ã¯ãã¯å¤æ°åã¨åè´ããããããã£ã®å¤ãå¤æ°ã«ä»£å
¥ãããã
å¤æ°å®£è¨å¤ã§åå²ä»£å ¥ãããéã®ãã©ãã
ãªãã¸ã§ã¯ãã®åå²ä»£å
¥ãè¡ãæãå
¨ä½ã()
ã§å²ã£ã¦ãªãã¨ã¨ã©ã¼ã«ãªã
å¤æ°å®£è¨ã¨ãªãã¸ã§ã¯ãã®åå²ä»£å
¥ãå¥ã«è¡ãå ´åã¯ãåå²ä»£å
¥ã®å¼å
¨ä½ã()
ã§å²ã£ã¦ãªãã¨SyntaxError: Unexpected token
let a, b; {a, b} = { a: 10, b: 20 }; console.log(a, b); // {a, b} = { a: 10, b: 20 } // ^ // SyntaxError: Unexpected token =
â åå²ä»£å
¥å
¨ä½ã()
ã§å²ã
let a, b; ({a, b} = { a: 10, b: 20 }); console.log(a, b); // 10 20
åå²ä»£å
¥ã®åã®æã«ã»ãã³ãã³;
ãç¡ãã¨ã¨ã©ã¼ã«ãªãå ´åããã
å¤æ°å®£è¨ã ãã ã¨ã¨ã©ã¼ã«ãªããªã
let a, b [a, b] = [1, 2] console.log(a, b) // 1, 2
å¤æ°å®£è¨æã«å¤ã代å
¥ãã¦ããã¨ReferenceError: X is not defined
let a = 1, b = 2 [a, b] = [b, a] console.log(a, b) // [a, b] = [b, a] // ^ // ReferenceError: b is not defined
â åã®æã«ã»ãã³ãã³;
ãããã°ã¨ã©ã¼ã«ãªããªã
let a = 1, b = 2; [a, b] = [b, a] console.log(a, b) // 2 1
åå²ä»£å
¥ã®ç´åã«ææ«ã«ã»ãã³ãã³;
ããªãconsole.log()
ã®ãããªæãããã¨TypeError
let a = 1, b = 2 console.log(a, b) [a, b] = [b, a] console.log(a, b) // [a, b] = [b, a] // ^ // TypeError: Cannot set property '2' of undefined
â åå²ä»£å
¥ç´åã®ææ«ã«ã»ãã³ãã³;
ãã¤ããã°OK
let a = 1, b = 2 console.log(a, b); // 1 2 [a, b] = [b, a] console.log(a, b) // 2 1
å¥ã«åã®æã®ææ«ã§ãªãã¦ããåå²ä»£å ¥å¼ã®ç´åã«ã»ãã³ãã³ãããã°OKã£ã½ã
let a = 1, b = 2 console.log(a, b) // 1 2 ; [a, b] = [b, a] console.log(a, b) // 2 1
åå²ä»£å
¥å¼ã®åã«;
ãæ¸ãã¦ãã¾ã£ã¦ãOK
let a = 1, b = 2 console.log(a, b) // 1 2 ;[a, b] = [b, a] console.log(a, b) // 2 1
åå²ä»£å
¥ã®ç´åããããã¯ã¨ããã»ãã³ãã³;
ã§æã®çµãããæ示ããã¦ããã°ã¨ã©ã¼ã«ãªããªãã£ã½ãï¼
let a = 1, b = 2 {} // function func() {} ã¨ãã§ãOK [a, b] = [b, a] console.log(a, b) // 2 1
ãªãã¸ã§ã¯ãã§ãåæ§ãåå²ä»£å
¥å¼ã®ç´åã;
ããããã¯ã§ãªãã¨ã¨ã©ã¼
let cute, cool ({cute, cool} = { cute: 'æå®®ãã¡ã', cool: 'é³åã»ã¤ã©' }) console.log(cute, cool) // æå®®ãã¡ã, é³åã»ã¤ã©
å¤æ°å®£è¨ã ããªãææ«ã«ã»ãã³ãã³;
ããªãã¦ãã¨ã©ã¼ã«ãªããªããã
å¤æ°å®£è¨æã«åæå¤ã®ä»£å
¥ãããã¨ã¨ã©ã¼ (TypeError)
let cute = 'æå®®ãã¡ã', cool = 'é§ç¢ããã' ({cute, cool} = { cute: '大空ããã', cool: 'æ°·ä¸ã¹ãã¬' }) // ({cute, cool} = { cute: '大空ããã', cool: 'æ°·ä¸ã¹ãã¬' }) // ^ // TypeError: "é§ç¢ããã" is not a function
â 代å
¥ãã¦ããæç« ãçµãã£ã¦ãªããåå²ä»£å
¥ã®å¼ãç¶ãã¦ãã¦é¢æ°ã ã¨å¤æããã¦ããã®ã§ãåå²ä»£å
¥å¼ã®åã«;
ãã¤ãããããããã¯ã«ãªãå¼ãå
¥ãã°OK
let cute = 'æå®®ãã¡ã', cool = 'é§ç¢ããã'; ({cute, cool} = { cute: '大空ããã', cool: 'æ°·ä¸ã¹ãã¬' }) console.log(cute, cool) // 大空ããã æ°·ä¸ã¹ãã¬
ã¨ã
let cute = 'æå®®ãã¡ã', cool = 'é§ç¢ããã', n = 5 while(n--) { console.log(n) } ({cute, cool} = { cute: '大空ããã', cool: 'æ°·ä¸ã¹ãã¬' }) console.log(cute, cool) // 大空ããã æ°·ä¸ã¹ãã¬
ãããã¯å¼ãããã¨ã
var cute = 'æå®®ãã¡ã', cool = 'é§ç¢ããã' ;({cute, cool} = { cute: '大空ããã', cool: 'æ°·ä¸ã¹ãã¬' }) console.log(cute, cool) // 大空ããã æ°·ä¸ã¹ãã¬
åå²ä»£å
¥å¼ã®å
é ã;
ã§å§ããã¨ã
åå²ä»£å
¥ã®åã«å
¥ãæããããã¯ã§ãªãå ´åã¯ææ«ã«ã»ãã³ãã³;
ãç¡ãã¨ã¨ã©ã¼ã«ãªã (TypeError)
function myFunc() { console.log('OKOKOK-') } var sexy = 'ç¥åç¾æ', pop = 'å¤æ¨¹ã¿ãã' myFunc() ({sexy, pop} = { sexy: '風沢ãã', pop: 'å´èãã' }) // ({sexy, pop} = { sexy: '風沢ãã', pop: 'å´èãã' }) // ^ // TypeError: myFunc(...) is not a function
â åå²ä»£å
¥ã®ç´åã«ã»ãã³ãã³;
ãããã°OK
function myFunc() { console.log('OKOKOK-') } var sexy = 'ç¥åç¾æ', pop = 'å¤æ¨¹ã¿ãã' myFunc(); ({sexy, pop} = { sexy: '風沢ãã', pop: 'å´èãã' }) console.log(sexy, pop) // 風沢ãã å´èãã
ã
ã¾ã¨ã
å¤æ°å®£è¨ã¨åæã§ãªãåå²ä»£å ¥ã¯ãä¸è¨ãã¿ã¼ã³ã§æç« ãç¶ãã¦ããã¨å¤æããã¦ã¨ã©ã¼ã«ãªãããã§ãã
- ãªãã¸ã§ã¯ãã®å ´åã¯åå²ä»£å
¥ã®å¼å
¨ä½ã
()
ã§å²ã£ã¦ãªãã¨ã¨ã©ã¼ã«ãªã - åå²ä»£å
¥å¼ã®ç´åããå¤æ°å®£è¨ã®ã¿ãåã¯ãã»ãã³ãã³
;
ããããã¯{}
ã§ãªãã¨ã¨ã©ã¼ã«ãªã
ã
æè¿ã®JSã®ã³ã¼ããESLintã§ãJSã§ã»ãã³ãã³;
ã¯ä¸è¦ã«ãªã£ãã¿ãããªå°è±¡ãåæã«åãã¦ãã®ã§ãããåºæ¬çã«å¿
è¦ãªææ«ã«ã¯ã»ãã³ãã³ä»ããããã«ããæ¹ãè¯ãããããªãããªã£ã¦æãã¾ããã
å¤æ°å®£è¨ã¨åæã§ãªãæã§ä½¿ãåå²ä»£å
¥å¼ã¯ã»ãã³ãã³;
ããå§ãããã¨ãã¨ãã¦ãè¯ãããã§ããããã®æ¹ãLintã¨ãã«æããããã§è¨å®ã¨ããé¢åããã§ãâ¦
[åè]
éç¿ECMAScript6: 次ä¸ä»£ã®æ¨æºJavaScriptãä»ãããã¹ã¿ã¼ï¼ éç¿ã·ãªã¼ãº
- ä½è :å±±ç°ç¥¥å¯
- çºå£²æ¥: 2015/08/29
- ã¡ãã£ã¢: Kindleç