Baseline Widely available
constructor
ë©ìëë í´ëì¤ì ì¸ì¤í´ì¤ ê°ì²´ë¥¼ ìì±íê³ ì´ê¸°ííë í¹ë³í ë©ìëì
ëë¤.
class Polygon {
constructor() {
this.name = "Polygon";
}
}
const poly1 = new Polygon();
console.log(poly1.name);
// Expected output: "Polygon"
구문
constructor() { ... }
constructor(argument0) { ... }
constructor(argument0, argument1) { ... }
constructor(argument0, argument1, ... , argumentN) { ... }
ì¤ëª
constructor
를 ì¬ì©íë©´ ë¤ë¥¸ 모ë ë©ìë í¸ì¶ë³´ë¤ ìì ìì ì¸, ì¸ì¤í´ì¤ ê°ì²´ë¥¼ ì´ê¸°íí ë ìíí ì´ê¸°í ì½ë를 ì ìí ì ììµëë¤.
class Person {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`Hello, my name is ${this.name}`);
}
}
const otto = new Person("Otto");
otto.introduce();
í´ëì¤ì ìì±ì를 ì ìíì§ ìì¼ë©´ 기본 ìì±ì를 ì¬ì©í©ëë¤. ì무ê²ë ììíì§ ìë 기본 í´ëì¤ì¼ ëì 기본 ìì±ìë ë¹ ë©ìëì ëë¤.
ë¤ë¥¸ í´ëì¤ë¥¼ ììíë ê²½ì°, 기본 ìì±ìë ìì ì 매ê°ë³ì를 ë¶ëª¨ í´ëì¤ì ìì±ìë¡ ì ë¬í©ëë¤.
constructor(...args) {
super(...args);
}
ë°ë¼ì ë¤ìê³¼ ê°ì ì½ë를 ìì±í ì ììµëë¤.
class ValidationError extends Error {
printCustomerMessage() {
return `Validation failed :-( (details: ${this.message})`;
}
}
try {
throw new ValidationError("Not a valid phone number");
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.name); // ValidationErrorê° ìëë¼ Error!
console.log(error.printCustomerMessage());
} else {
console.log("Unknown error", error);
throw error;
}
}
ValidationError
í´ëì¤ë ìë¬´ë° ì´ê¸°í ëìë íìíì§ ìì¼ë¯ë¡ ìì±ì를 ë³ëë¡ ëª
ìíì§ ììì¼ë©°, ëì 기본 ìì±ìê° ë§¤ê°ë³ìë¡ ë¶ëª¨ Error
í´ëì¤ì ì´ê¸°í를 ì²ë¦¬íê³ ììµëë¤.
ê·¸ë¬ë, íì í´ëì¤ì ì§ì ìì±ì를 ì ìí ê²½ì°, ë¶ëª¨ í´ëì¤ì ìì±ì를 í¸ì¶íë ¤ë©´ ì§ì super()
를 í¸ì¶í´ì¼ í©ëë¤.
class ValidationError extends Error {
constructor(message) {
super(message); // ë¶ëª¨ í´ëì¤ì ìì±ì í¸ì¶
this.name = "ValidationError";
this.code = "42";
}
printCustomerMessage() {
return `Validation failed :-( (details: ${this.message}, code: ${this.code})`;
}
}
try {
throw new ValidationError("Not a valid phone number");
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.name); // ì´ì ValidationError!
console.log(error.printCustomerMessage());
} else {
console.log("Unknown error", error);
throw error;
}
}
"constructor
"ë¼ë ì´ë¦ì ë©ìëë íëì í´ëì¤ì ì¤ì§ íëë§ ì¡´ì¬í ì ììµëë¤. ë ê° ì´ìì constructor
ë©ìë를 ì ìíë©´ SyntaxError
ê° ë°ìí©ëë¤.
constructor
ë©ìë ì¬ì©í기
class Square extends Polygon {
constructor(length) {
// lengthë¡ ë¤ê°íì ëì´ì ëì´ë¥¼ ì ìí기 ìí´ ë¶ëª¨ í´ëì¤ì ìì±ì를 í¸ì¶í©ëë¤.
super(length, length);
// ì°¸ê³ : íì í´ëì¤ìì, this를 ì¬ì©í기 ì ìë ë°ëì super()를 먼ì í¸ì¶í´ì¼ í©ëë¤.
// ê·¸ë ì§ ìì¼ë©´ ReferenceErrorê° ë°ìí©ëë¤.
this.name = "Square";
}
get area() {
return this.height * this.width;
}
set area(value) {
this.height = value ** 0.5;
this.width = value ** 0.5;
}
}
ë¤ë¥¸ ìì
ìë ìì ìì, Square
í´ëì¤ì íë¡í íì
ì Rectangle
ì íë¡í íì
ì¼ë¡ ë°ê¾¼ íìë, Square
ì ì¸ì¤í´ì¤ë¥¼ ìì±í ë ë¶ëª¨ í´ëì¤ì¸ Polygon
ìì±ì를 í¸ì¶íë ê²ì íì¸í ì ììµëë¤.
class Polygon {
constructor() {
this.name = "Polygon";
}
}
class Square extends Polygon {
constructor() {
super();
}
}
class Rectangle {}
Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false
console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true
let newInstance = new Square();
console.log(newInstance.name); //Polygon
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
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