Baseline Widely available
구조 ë¶í´ í ë¹ êµ¬ë¬¸ì ë°°ì´ì´ë ê°ì²´ì ìì±ì í´ì²´íì¬ ê·¸ ê°ì ê°ë³ ë³ìì ë´ì ì ìê² íë JavaScript ííìì ëë¤.
ìëí´ ë³´ê¸°let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// Expected output: 10
console.log(b);
// Expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// Expected output: Array [30, 40, 50]
구문
var a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20
// Stage 4(finished) proposal
({ a, b, ...rest } = { a: 10, b: 20, c: 30, d: 40 });
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
ì¤ëª
ê°ì²´ ë° ë°°ì´ ë¦¬í°ë´ ííìì ì¬ì©íë©´ ì¦ììì ì½ê² ë°ì´í° ëì¹ë¥¼ ë§ë¤ ì ììµëë¤.
구조 ë¶í´ í ë¹ì 구문ì ìì ë¹ì·íì§ë§, ëì í ë¹ë¬¸ì ì¢ë³ìì ì¬ì©íì¬, ìë ë³ììì ì´ë¤ ê°ì ë¶í´í´ í ë¹í ì§ ì ìí©ëë¤.
var x = [1, 2, 3, 4, 5];
var [y, z] = x;
console.log(y); // 1
console.log(z); // 2
구조 ë¶í´ í ë¹ì Perlì´ë Python ë± ë¤ë¥¸ ì¸ì´ê° ê°ì§ê³ ìë 기ë¥ì ëë¤.
ë°°ì´ êµ¬ì¡° ë¶í´ 기본 ë³ì í ë¹var foo = ["one", "two", "three"];
var [red, yellow, green] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // "three"
ì ì¸ìì ë¶ë¦¬í í ë¹
ë³ìì ì ì¸ì´ ë¶ë¦¬ëì´ë 구조 ë¶í´ë¥¼ íµí´ ê°ì í ë¹í ì ììµëë¤.
var a, b;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
기본ê°
ë³ìì 기본ê°ì í ë¹íë©´, ë¶í´í ê°ì´ undefined
ì¼ ë ê·¸ ê°ì ëì ì¬ì©í©ëë¤.
var a, b;
[a = 5, b = 7] = [1];
console.log(a); // 1
console.log(b); // 7
ë³ì ê° êµíí기
íëì 구조 ë¶í´ ííìë§ì¼ë¡ ë ë³ìì ê°ì êµíí ì ììµëë¤.
구조 ë¶í´ í ë¹ ìì´ ë ê°ì êµííë ¤ë©´ ìì ë³ìê° íìí©ëë¤. (ì¼ë¶ ë¡ì° ë 벨 ì¸ì´ììë XOR êµì²´ í¸ë¦ì ì¬ì©í ì ììµëë¤)
var a = 1;
var b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
í¨ìê° ë°íí ë°°ì´ ë¶ì
í¨ìë ì´ì ë¶í° ë°°ì´ì ë°íí ì ìììµëë¤. 구조 ë¶í´ë¥¼ ì¬ì©íë©´ ë°íë ë°°ì´ì ëí ìì ì ë ê°ê²°íê² ìíí ì ììµëë¤.
ìë ìì ìì f()
ë ì¶ë ¥ì¼ë¡ ë°°ì´ [1, 2]
ì ë°ííëë°, íëì 구조 ë¶í´ë§ì¼ë¡ ê°ì ë¶ìí ì ììµëë¤.
function f() {
return [1, 2];
}
var a, b;
[a, b] = f();
console.log(a); // 1
console.log(b); // 2
ì¼ë¶ ë°í ê° ë¬´ìí기
ë¤ìê³¼ ê°ì´ íìíì§ ìì ë°í ê°ì 무ìí ì ììµëë¤.
function f() {
return [1, 2, 3];
}
var [a, , b] = f();
console.log(a); // 1
console.log(b); // 3
ë°í ê°ì 모ë 무ìí ìë ììµëë¤.
ë³ìì ë°°ì´ì ë머ì§ë¥¼ í ë¹í기배ì´ì 구조 ë¶í´í ê²½ì°, ëë¨¸ì§ êµ¬ë¬¸ì ì´ì©í´ ë¶í´íê³ ë¨ì ë¶ë¶ì íëì ë³ìì í ë¹í ì ììµëë¤.
var [a, ...b] = [1, 2, 3];
console.log(a); // 1
console.log(b); // [2, 3]
ëë¨¸ì§ ììì ì¤ë¥¸ìª½ ë¤ì ì¼íê° ìì¼ë©´ SyntaxError
ê° ë°ìí©ëë¤.
var [a, ...b,] = [1, 2, 3];
// SyntaxError: rest element may not have a trailing comma
ì ê· ííìê³¼ ì¼ì¹íë ê° í´ì²´í기
ì ê· ííìì exec()
ë©ìëë ì¼ì¹íë ë¶ë¶ë¥¼ ì°¾ì¼ë©´ ê·¸ 문ìì´ìì ì ê·ìê³¼ ì¼ì¹íë ë¶ë¶ ì 체를 ë°°ì´ì 맨 ìì, ê·¸ë¦¬ê³ ê·¸ ë¤ì ì ê·ììì ê´í¸ë¡ ë¬¶ì¸ ê° ê·¸ë£¹ê³¼ ì¼ì¹íë ë¶ë¶ì í¬í¨íë ë°°ì´ì ë°íí©ëë¤. 구조 ë¶í´ í ë¹ì íìíì§ ìì ê²½ì° ì¼ì¹íë ì ì²´ ë¶ë¶ì 무ìíê³ íìí ë¶ë¶ë§ ì½ê² ë¹¼ì¬ ì ììµëë¤.
function parseProtocol(url) {
var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
if (!parsedURL) {
return false;
}
console.log(parsedURL); // ["https://developer.mozilla.org/en-US/Web/JavaScript", "https", "developer.mozilla.org", "en-US/Web/JavaScript"]
var [, protocol, fullhost, fullpath] = parsedURL;
return protocol;
}
console.log(
parseProtocol("https://developer.mozilla.org/en-US/Web/JavaScript"),
); // "https"
ê°ì²´ 구조 ë¶í´ 기본 í ë¹
var o = { p: 42, q: true };
var { p, q } = o;
console.log(p); // 42
console.log(q); // true
ì ì¸ ìë í ë¹
구조 ë¶í´ë¥¼ íµí´ ë³ìì ì ì¸ê³¼ ë¶ë¦¬íì¬ ë³ìì ê°ì í ë¹í ì ììµëë¤.
var a, b;
({ a, b } = { a: 1, b: 2 });
ì°¸ê³ : í ë¹ ë¬¸ì ëë¬ì¼ ( .. )
ë ì ì¸ ìì´ ê°ì²´ 리í°ë´(object literal) ë¹êµ¬ì¡°í í ë¹ì ì¬ì©í ë íìí 구문ì
ëë¤.
{a, b} = {a:1, b:2}
ë ì í¨í ë
립 êµ¬ë¬¸ì´ ìëëë¤. ì¢ë³ì {a, b}
ì´ ê°ì²´ 리í°ë´ì´ ìë ë¸ë¡ì¼ë¡ ê°ì£¼ë기 ë문ì
ëë¤.
íì§ë§, ({a, b} = {a:1, b:2})
ë ì í¨íë°, var {a, b} = {a:1, b:2}
ì ê°ìµëë¤.
( .. )
ííì ììë ì¸ë¯¸ì½ë¡ ì´ ìì´ì¼ í©ëë¤. ê·¸ë ì§ ìì ê²½ì° ì´ì ì¤ê³¼ ì°ê²°ëì´ í¨ì를 ì¤ííëë° ì´ì©ë ì ììµëë¤.
ê°ì²´ë¡ë¶í° ìì±ì í´ì²´íì¬ ê°ì²´ì ìë ìì±ëª ê³¼ë ë¤ë¥¸ ì´ë¦ì ë³ìì í ë¹í ì ììµëë¤.
var o = { p: 42, q: true };
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
기본ê°
ê°ì²´ë¡ë¶í° í´ì²´ë ê°ì´ undefined
ì¸ ê²½ì°, ë³ìì 기본ê°ì í ë¹í ì ììµëë¤.
var { a = 10, b = 5 } = { a: 3 };
console.log(a); // 3
console.log(b); // 5
ê¸°ë³¸ê° ê°ë ìë¡ì´ ì´ë¦ì ë³ìì í ë¹í기
ìë¡ì´ ë³ìëª í ë¹ê³¼ ê¸°ë³¸ê° í ë¹ì íë²ì í ì ììµëë¤.
var { a: aa = 10, b: bb = 5 } = { a: 3 };
console.log(aa); // 3
console.log(bb); // 5
í¨ì 매ê°ë³ìì ê¸°ë³¸ê° ì¤ì í기 ES5 ë²ì
function drawES5Chart(options) {
options = options === undefined ? {} : options;
var size = options.size === undefined ? "big" : options.size;
var cords = options.cords === undefined ? { x: 0, y: 0 } : options.cords;
var radius = options.radius === undefined ? 25 : options.radius;
console.log(size, cords, radius);
// ì´ì ëëì´ ì°¨í¸ ê·¸ë¦¬ê¸° ìí
}
drawES5Chart({
cords: { x: 18, y: 30 },
radius: 30,
});
ES2015 ë²ì
function drawES2015Chart({
size = "big",
cords = { x: 0, y: 0 },
radius = 25,
} = {}) {
console.log(size, cords, radius);
// ì°¨í¸ ê·¸ë¦¬ê¸° ìí
}
drawES2015Chart({
cords: { x: 18, y: 30 },
radius: 30,
});
ìì drawES2015Chart
í¨ìì ìíìì 구조 ë¶í´ë ì¢ë³ì ë¹ ì¤ë¸ì í¸ ë¦¬í°ë´ì í ë¹íë ê²ì ë³¼ ì ììµëë¤ {size = 'big', cords = {x: 0, y: 0}, radius = 25} = {}
. ë¹ ì¤ë¸ì í¸ë¥¼ ì°ë³ì í ë¹íì§ ìê³ ë í¨ì를 ìì±í ì ììµëë¤. íì§ë§, ì§ê¸ì ííììë ë¨ìí drawES2015Chart()
ì ê°ì´ ì´ë¤ 매ê°ë³ì ìì´ë í¸ì¶í ì ìì§ë§, ì°ë³ì ë¹ ì¤ë¸ì í¸ í ë¹ì ìì¤ë¤ë©´ í¨ì í¸ì¶ì ì ì´ë íëì ì¸ìê° ì ê³µëì´ì¼ë§ í©ëë¤. ì´ í¨ìê° ì´ë í 매ê°ë³ì ìì´ë í¸ì¶í ì ì길 ìíë¤ë©´ ì§ê¸ ííê° ì ì©íê³ , 무조건 ê°ì²´ë¥¼ ë기길 ìíë ê²½ì°ìë ë¹ ê°ì²´ í ë¹ì íì§ ìë ê²ì´ ì ì©í ì ììµëë¤.
var metadata = {
title: "Scratchpad",
translations: [
{
locale: "de",
localization_tags: [],
last_edit: "2014-04-14T08:43:37",
url: "/de/docs/Tools/Scratchpad",
title: "JavaScript-Umgebung",
},
],
url: "/ko/docs/Tools/Scratchpad",
};
var {
title: englishTitle,
translations: [{ title: localeTitle }],
} = metadata;
console.log(englishTitle); // "Scratchpad"
console.log(localeTitle); // "JavaScript-Umgebung"
for of ë°ë³µë¬¸ê³¼ 구조 ë¶í´
var people = [
{
name: "Mike Smith",
family: {
mother: "Jane Smith",
father: "Harry Smith",
sister: "Samantha Smith",
},
age: 35,
},
{
name: "Tom Jones",
family: {
mother: "Norah Jones",
father: "Richard Jones",
brother: "Howard Jones",
},
age: 25,
},
];
for (var {
name: n,
family: { father: f },
} of people) {
console.log("Name: " + n + ", Father: " + f);
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
í¨ì 매ê°ë³ìë¡ ì ë¬ë ê°ì²´ìì íë í´ì²´í기
function userId({ id }) {
return id;
}
function whois({ displayName: displayName, fullName: { firstName: name } }) {
console.log(displayName + " is " + name);
}
var user = {
id: 42,
displayName: "jdoe",
fullName: {
firstName: "John",
lastName: "Doe",
},
};
console.log("userId: " + userId(user)); // "userId: 42"
whois(user); // "jdoe is John"
ì´ ìì ë user ê°ì²´ë¡ë¶í° id
, displayName
ë° firstName
ì í´ì²´í´ ì¶ë ¥í©ëë¤.
ê³ì°ë ìì± ì´ë¦(computed property name)ì, ê°ì²´ 리í°ë´ê³¼ ë¹ì·íê² êµ¬ì¡° ë¶í´ìë ì¬ì©ë ì ììµëë¤.
let key = "z";
let { [key]: foo } = { z: "bar" };
console.log(foo); // "bar"
ê°ì²´ 구조 ë¶í´ìì Rest
Rest/Spread Properties for ECMAScript ì ì(stage 3)ììë 구조 ë¶í´ì rest 구문ì ì¶ê°íê³ ììµëë¤. rest ìì±ì 구조 ë¶í´ í¨í´ì¼ë¡ 걸ë¬ì§ì§ ìì ì´ê±°í ìì±ì í¤ë¥¼ ê°ì§ ëë¨¸ì§ í목ë¤ì 모ìëë¤.
let { a, b, ...rest } = { a: 10, b: 20, c: 30, d: 40 };
a; // 10
b; // 20
rest; // { c: 30, d: 40 }
ìì± ì´ë¦ì´ ì í¨í JavaScript ìë³ìëª
ì´ ìë ê²½ì°
구조 ë¶í´ë JavaScript ìë³ì ì´ë¦ì¼ë¡ ì í©íì§ ìì ìì±ëª ì´ ì ê³µë ê²½ì°ìë ì´ì©í ì ììµëë¤. ì´ ëë ëì²´í ì í¨í ìë³ìëª ì ì ê³µí´ì¼ í©ëë¤.
const foo = { "fizz-buzz": true };
const { "fizz-buzz": fizzBuzz } = foo;
console.log(fizzBuzz); // "true"
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4