728x90 300x250 SMALL Constructor2 ES6 클래스 상속에서의 constructor ES6에서는 클래스와 extends키워드를 사용하여 좀 더 명싱적이고 간결한 형태로 구현할 수 있다. ES6의 클래스 상속을 사용하는 경우, 자식클래스의 인스턴스가 자신을 생성한 생성자 함수를 참조하는 'constructor'속성은 자식 클래스를 가리킨다. class Parent { constructor() { // ... } } class Child extends Parent { constructor() { super(); // ... } } const myChild = new Child(); console.log(myChild.constructor); // Child 생성자 함수를 참조 ES6클래스 상속에서 특별히 자식 클래스를 가리키도로 설계되었기 때문에 myChild.constructor는 Chi.. 2024. 2. 14. constructor 속성 변경 JS에서 모든 객체는 'constructor'라는 속성을 갖는다. 이 속성은 객체를 생성한 생성자(constructor)함수를 가리킨다. 객체를 만들 때 사용된 함수가 무엇이었는지를 나타내는데 유용하다. 예를들어, 다음과 같은 객체가 있을 경우 function Person(name) { this.name = name; } var john = new Person("John"); 여기서 'john'객체는 'Person'생성자 함수를 통해 생성되었다. 이 객체의 'constructor'속성은 'Person'함수를 가리킨다. 즉 'john.constructor'는 'Person'이 된다. 하지만, 프로토타입 체인을 사용하여 상속을 구현하면 일부 예상치 못한 동작이 발생할 수 있다. MyChild.prototyp.. 2024. 1. 17. 이전 1 다음 728x90 반응형 LIST