ã¡ã½ããå®ç¾©
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´9æ.
ã¡ã½ããå®ç¾©ã¯ããªãã¸ã§ã¯ãåæååå ã§é¢æ°ããããã£ãå®ç¾©ããããã®ç°¡ç¥åãããæ§æã§ããã¾ããã¯ã©ã¹å ã§ã使ç¨ã§ãã¾ãã
試ãã¦ã¿ã¾ããã
const obj = {
foo() {
return "bar";
},
};
console.log(obj.foo());
// äºæ³ãããçµæ: "bar"
æ§æ
({
property(弿°) {},
*generator(弿°) {},
async property(弿°) {},
async *generator(弿°) {},
// è¨ç®ããããã¼
[å¼](弿°) {},
*[å¼](弿°) {},
async [å¼](弿°) {},
async *[å¼](弿°) {},
})
解説
ç°¡ç¥æ§æã¯ã²ãã¿ã¼ãã»ãã¿ã¼æ§æã«ä¼¼ã¦ãã¾ãã
以ä¸ã®ãããªã³ã¼ãããã£ãã¨ãã¾ãã
const obj = {
foo: function () {
// â¦
},
bar: function () {
// â¦
},
};
ããã以ä¸ã®ããã«ç縮ãããã¨ãã§ãã¾ãã
const obj = {
foo() {
// â¦
},
bar() {
// â¦
},
};
ãã®æ§æã使ç¨ãã¦å®ç¾©ãããããããã£ã¯ã使ããããªãã¸ã§ã¯ãã®èªèº«ã®ããããã£ã§ãããé常ã®ããããã£ã¨åæ§ã«æ§æä¸å¯ãåæå¯è½ãæ¸ãè¾¼ã¿å¯è½ã§ãã
function*ãasync functionãasync function* ã®ããããã£ã¯ãã¹ã¦åºæã®ã¡ã½ããæ§æãããã¾ããä¸è¨ã«ããä¾ãåç
§ãã¦ãã ããã
ãã ããã¡ã½ããæ§æã¯ãå¤ã¨ãã¦é¢æ°ãæã¤é常ã®ããããã£ã¨ã¯åçã§ã¯ãªããã¨ã«æ³¨æãã¦ãã ãããæå³è«çã«ãéããããã¾ããããã«ããããªãã¸ã§ã¯ããªãã©ã«ã§å®ç¾©ãããã¡ã½ããã¯ãã¯ã©ã¹å ã®ã¡ã½ããã¨ããä¸è²«æ§ãæã¤ããã«ãªãã¾ãã
ã¡ã½ããå®ç¾©ã¯æ§ç¯å¯è½ã§ã¯ãªã
ã¡ã½ããã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ã«ã¯ãªãã¾ãããã³ã³ã¹ãã©ã¯ã¿ã¼ã¨ãã¦ã¤ã³ã¹ã¿ã³ã¹åãããã¨ããã¨ã TypeError ãçºçãã¾ãã 䏿¹ã颿°ã¨ãã¦ä½æãããããããã£ã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ã¨ãã¦ä½¿ç¨ã§ãã¾ãã
const obj = {
method() {},
};
new obj.method(); // TypeError: obj.method is not a constructor
ã¡ã½ããå®ç¾©å ã§ã® super ã®ä½¿ç¨
ã¡ã½ããã¨ãã¦å®ç¾©ããã颿°ã®ã¿ã super ãã¼ã¯ã¼ãã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã super.prop ã¯ãã¡ã½ãããåæåããããªãã¸ã§ã¯ãã®ãããã¿ã¤ãä¸ã§ããããã£ãæ¤ç´¢ãã¾ãã
const obj = {
__proto__: {
prop: "foo",
},
notAMethod: function () {
console.log(super.prop); // SyntaxError: 'super' keyword unexpected here
},
};
ä¾
>ã¡ã½ããå®ç¾©ã®ä½¿ç¨
const obj = {
a: "foo",
b() {
return this.a;
},
};
console.log(obj.b()); // "foo"
ã¯ã©ã¹å ã§ã®ã¡ã½ããå®ç¾©
ã¾ã£ããåãæ§æã使ç¨ãã¦ãã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã§å©ç¨ã§ãããããªãã¯ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå®ç¾©ãããã¨ãã§ãã¾ããã¯ã©ã¹å ã§ã¯ãã¡ã½ããéãåºåãã«ã³ãã¯å¿ è¦ããã¾ããã
class ClassWithPublicInstanceMethod {
publicMethod() {
return "hello world";
}
secondPublicMethod() {
return "goodbye world";
}
}
const instance = new ClassWithPublicInstanceMethod();
console.log(instance.publicMethod()); // "hello world"
ãããªãã¯ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¯ã¯ã©ã¹ã® prototype ããããã£ä¸ã«å®ç¾©ãããããããã®ã¯ã©ã¹ã®ãã¹ã¦ã¤ã³ã¹ã¿ã³ã¹ã§å
±æããã¾ãããããã¯æ¸ãè¾¼ã¿å¯è½ãåæä¸å¯ãæ§æä¸å¯ã§ãã
ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããå
ã§ã¯ãthis 㨠super ã¯é常ã®ã¡ã½ããã¨åæ§ã«åä½ãã¾ããé常ãthis ã¯ã¤ã³ã¹ã¿ã³ã¹èªä½ãåç
§ãã¾ãããµãã¯ã©ã¹ã§ã¯ãsuper ã使ç¨ãããã¨ã§ãã¡ã½ãããæ·»ä»ããããªãã¸ã§ã¯ãã®ãããã¿ã¤ãã«ã¢ã¯ã»ã¹ã§ããã¹ã¼ãã¼ã¯ã©ã¹ã®ã¡ã½ãããå¼ã³åºããã¨ãã§ãã¾ãã
class BaseClass {
msg = "hello world";
basePublicMethod() {
return this.msg;
}
}
class SubClass extends BaseClass {
subPublicMethod() {
return super.basePublicMethod();
}
}
const instance = new SubClass();
console.log(instance.subPublicMethod()); // "hello world"
éçã¡ã½ããã¨ãã©ã¤ãã¼ãã¡ã½ããã¯ä¼¼ãæ§æã使ç¨ãã¾ããããã㯠static ããã³ãã©ã¤ãã¼ãè¦ç´ ã®ãã¼ã¸ã§èª¬æããã¦ãã¾ãã
ç®åºããããã£å
ã¡ã½ããæ§æã¯ç®åºããããã£åã«ã対å¿ãã¦ãã¾ãã
const bar = {
foo0: function () {
return 0;
},
foo1() {
return 1;
},
["foo".toUpperCase()]() {
return 2;
},
};
console.log(bar.foo0()); // 0
console.log(bar.foo1()); // 1
console.log(bar.FOO()); // 2
ã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ãã
ã¸ã§ãã¬ã¼ã¿ã¼ããããã£åã®åã«ã¢ã¹ã¿ãªã¹ã¯ (*) ãå¿
è¦ã§ãããã¨ã«æ³¨æãã¦ãã ãããï¼ããªãã¡ã * g(){} ã¯åä½ãã¾ããã g *(){} ã¯åä½ãã¾ãããï¼
// ååä»ãããããã£ã使ç¨
const obj = {
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 obj = {
f: async function () {
await somePromise;
},
};
// ç°¡ç¥æ§æã使ç¨ãã¦åããªãã¸ã§ã¯ããçæ
const obj2 = {
async f() {
await somePromise;
},
};
éåæã¸ã§ãã¬ã¼ã¿ã¼ã¡ã½ãã
// ååä»ãããããã£ã®ä½¿ç¨
const obj = {
f: async function* () {
yield 1;
yield 2;
yield 3;
},
};
// ç°¡ç¥æ§æã使ç¨ãã¦åããªãã¸ã§ã¯ããçæ
const obj2 = {
async *f() {
yield 1;
yield 2;
yield 3;
},
};
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-method-definitions> |