Baseline Widely available
ã¯ã©ã¹å¼ã¯ã ECMAScript 2015 ã§ã¯ã©ã¹ãå®ç¾©ããæ¹æ³ã® 1 ã¤ã§ãã颿°å¼ã¨åãããã«ãã¯ã©ã¹å¼ã¯ååãä»ãããã¨ãä»ããªããã¨ãã§ãã¾ããååãä»ããå ´åãã¯ã©ã¹åã¯ã¯ã©ã¹å é¨ã®ã¿ã®ãã¼ã«ã«ã§ãã
JavaScript ã®ã¯ã©ã¹ã¯ãããã¿ã¤ããã¼ã¹ã®ç¶æ¿ã使ããã¾ãã
試ãã¦ã¿ã¾ãããconst Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
area() {
return this.height * this.width;
}
};
console.log(new Rectangle(5, 8).area());
// Expected output: 40
æ§æ
const MyClass = class [className] [extends otherClassName] {
// ã¯ã©ã¹æ¬ä½
}
解説
ã¯ã©ã¹å¼ã®æ§æã¯ãã¯ã©ã¹å®£è¨ï¼æï¼ ã¨ä¼¼ã¦ãã¾ãã class
æã§ã¯ã class
å¼ã®æ¬ä½ã峿 ¼ã¢ã¼ãã§å®è¡ããã¾ãã
ããããã¯ã©ã¹å¼ã¨ã¯ã©ã¹æã§ã¯ããã¤ãã®ç¸éç¹ãããã¾ãã
SyntaxError
ãçºçãããã«ã¯ã©ã¹ãå宣è¨ãããã¨ãã§ãã¾ããããã¯ã¯ã©ã¹æã®å ´åã¯ã§ãã¾ãããconstructor
ã¡ã½ããã¯çç¥å¯è½ã§ããã¯ã©ã¹å¼ã§çæãããã¯ã©ã¹ã¯ã常㫠typeof
ã "function
" ã®å¤ãè¿ãã¾ãã
"use strict";
let Foo = class {}; // ã³ã³ã¹ãã©ã¯ã¿ã¼ããããã£ã¯çç¥å¯è½
Foo = class {}; // å宣è¨ãå¯è½
typeof Foo; // "function" ãè¿ã
typeof class {}; // "function" ãè¿ã
Foo instanceof Object; // true
Foo instanceof Function; // true
class Foo {} // SyntaxError ãçºç (ã¯ã©ã¹å®£è¨ã¯å宣è¨ãã§ããªã)
ä¾ ç°¡åãªã¯ã©ã¹å¼
以ä¸ã¯ãååã®ãªãç°¡åãªã¯ã©ã¹å¼ã§ãã夿° Foo
ã使ã£ã¦åç
§ã§ãã¾ãã
const Foo = class {
constructor() {}
bar() {
return "Hello World!";
}
};
const instance = new Foo();
instance.bar(); // "Hello World!"
Foo.name; // "Foo"
ååä»ãã¯ã©ã¹å¼
ã¯ã©ã¹å é¨ã§ç¾å¨ã®ã¯ã©ã¹ãåç §ãããå ´åã¯ãååä»ãã¯ã©ã¹å¼ã使ãã¦ãã ããããã®ååã¯ããã®ã¯ã©ã¹å¼èªèº«ã®ã¹ã³ã¼ãå ã ãã§è¦ããã¨ãã§ãã¾ãã
const Foo = class NamedFoo {
constructor() {}
whoIsThere() {
return NamedFoo.name;
}
};
const bar = new Foo();
bar.whoIsThere(); // "NamedFoo"
NamedFoo.name; // ReferenceError: NamedFoo is not defined
Foo.name; // "NamedFoo"
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
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