Baseline Widely available
ECMAScript 2015 ããããªãã¸ã§ã¯ãã¤ãã·ã£ã©ã¤ã¶ã¼ã®ã¡ã½ããå®ç¾©ã®ããã®çãæ§æãå°å ¥ããã¾ãããããã¯ãã¡ã½ããã®ååã«å²ãå½ã¦ããã颿°ã®ç°¡ç¥æ§æã§ãã
試ãã¦ã¿ã¾ãããconst obj = {
foo() {
return "bar";
},
};
console.log(obj.foo());
// Expected output: "bar"
æ§æ
const obj = {
get property() {},
set property(value) {},
property( parameters⦠) {},
*generator( parameters⦠) {},
async property( parameters⦠) {},
async* generator( parameters⦠) {},
// ç®åºããããã¼ã使ç¨å¯è½:
get [property]() {},
set [property](value) {},
[property]( parameters⦠) {},
*[generator]( parameters⦠) {},
async [property]( parameters⦠) {},
async* [generator]( parameters⦠) {},
};
解説
ç°¡ç¥æ§æã¯ãECMAScript 第 5 çã§å°å ¥ãããã²ãã¿ã¼ãã»ãã¿ã¼æ§æã«ä¼¼ã¦ãã¾ãã
以ä¸ã®ãããªã³ã¼ãããã£ãã¨ãã¾ãã
const obj = {
foo: function () {
// ...
},
bar: function () {
// ...
},
};
ããã以ä¸ã®ããã«ç縮ãããã¨ãã§ãã¾ãã
const obj = {
foo() {
// ...
},
bar() {
// ...
},
};
ã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ãã
ã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ããããåæ§ã«ç°¡ç¥æ§æã使ç¨ãã¦å®ç¾©ãããã¨ãã§ãã¾ãã
次ã®ããã«è¡ãã¾ãã
*
) ãå¿
è¦ã§ããããªãã¡ã * g(){}
ã¯åä½ãã¾ããã g *(){}
ã¯åä½ãã¾ãããyield
ãã¼ã¯ã¼ããå
¥ãããã¨ã¯ã§ãã¾ãããã¤ã¾ã æ§å¼ã®ã¸ã§ãã¬ã¼ã¿ã¼é¢æ°ã¯åä½ããã SyntaxError
ãçºçãã¾ãã yield
ã¯å¸¸ã«ã¢ã¹ã¿ãªã¹ã¯ (*
) ã¨ä¸ç·ã«ä½¿ã£ã¦ãã ããã// ååä»ãããããã£ã使ç¨
const obj2 = {
g: function* () {
let index = 0;
while (true) {
yield index++;
}
},
};
// ç°¡ç¥æ§æã使ç¨ãã¦åããªãã¸ã§ã¯ããçæ
const obj2 = {
*g() {
let index = 0;
while (true) {
yield index++;
}
},
};
const it = obj2.g();
console.log(it.next().value); // 0
console.log(it.next().value); // 1
éåæã¡ã½ãã
éåæã¡ã½ãããç°¡ç¥æ§æã使ç¨ãã¦å®ç¾©ãããã¨ãã§ãã¾ãã
// ååä»ãããããã£
const obj3 = {
f: async function () {
await some_promise;
},
};
// ç°¡ç¥æ§æã使ç¨ãã¦åããªãã¸ã§ã¯ããçæ
const obj3 = {
async f() {
await some_promise;
},
};
éåæã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ãã
ã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ãããéåæé¢æ°ã«ãããã¨ãã§ãã¾ãã
const obj4 = {
f: async function* () {
yield 1;
yield 2;
yield 3;
},
};
// ç°¡ç¥æ§æã使ç¨ãã¦åããªãã¸ã§ã¯ããçæ
const obj4 = {
async *f() {
yield 1;
yield 2;
yield 3;
},
};
ã¡ã½ããå®ç¾©ã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãªã
ã¡ã½ãããã³ã³ã¹ãã©ã¯ã¿ã¼ã«ãªããã¨ã¯ã§ãã¾ãããã¤ã³ã¹ã¿ã³ã¹åãããã¨ãã㨠TypeError
ãçºçãã¾ãã
const objA = {
method() {},
};
new objA.method(); // TypeError: obj.method is not a constructor
const objB = {
*g() {},
};
new objB.g(); // TypeError: obj.g is not a constructor (ES2016 ã§å¤æ´)
ä¾ ç°¡åãªãã¹ãã±ã¼ã¹
const obj = {
a: "foo",
b() {
return this.a;
},
};
console.log(obj.b()); // "foo"
è¨ç®ãããããããã£å
ç°¡ç¥æ§æã¯è¨ç®ãããããããã£åã«ã対å¿ãã¦ãã¾ãã
const bar = {
foo0: function () {
return 0;
},
foo1() {
return 1;
},
["foo" + 2]() {
return 2;
},
};
console.log(bar.foo0()); // 0
console.log(bar.foo1()); // 1
console.log(bar.foo2()); // 2
// A global function
function foo() {
return 1;
}
let name = "foo";
console.log(window[name]()); // 1
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
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