--- id: javascript-class-inheritance title: "JavaScript Class Inheritance" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["extends", "super", "getters and setters", "class hoisting", "subclass"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "classes", "inheritance", "oop"] raw_sources: ["https://www.w3schools.com/js/js_class_inheritance.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Class Inheritance]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Use `extends` to make one class inherit all methods of another and `super()` to reach the parent's constructor; classes also support `get`/`set` accessors and, unlike functions, are not hoisted. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`extends` for inheritance** β€” a class created with `extends` acquires all methods from another class. [S1] - **`super()` references the parent** β€” calling `super()` in the constructor accesses parent properties and methods. [S1] - **Inheritance aids reuse** β€” inheritance is useful for code reusability. [S1] - **Getters and setters** β€” classes support `get` and `set` keywords for special property handling. [S1] - **Getter access has no parentheses** β€” even though the getter is a method, you do not use parentheses to get the property value. [S1] - **Classes are not hoisted** β€” unlike functions, classes must be declared before use. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Subclass = `extends` + `super(...)`** β€” pass parent constructor arguments through `super()`, then add subclass-specific state. [S1] - **Underscore backing field for accessors** β€” store the value in `_carname` and expose it via `get carname()` / `set carname(x)` to avoid name collision with the accessor. [S1] - **Declare before use** β€” because classes aren't hoisted, place the declaration ahead of any `new`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Class Inheritance** To create class inheritance, use the `extends` keyword; the subclass acquires all methods of the parent. `super()` refers to the parent class, and calling it in the constructor accesses parent properties and methods. Inheritance is useful for code reusability: [S1] ```javascript class Car { constructor(brand) { this.carname = brand; } present() { return 'I have a ' + this.carname; } } class Model extends Car { constructor(brand, mod) { super(brand); this.model = mod; } show() { return this.present() + ', it is a ' + this.model; } } let myCar = new Model("Ford", "Mustang"); document.getElementById("demo").innerHTML = myCar.show(); ``` **Getters and Setters** Classes support getters and setters using the `get` and `set` keywords: [S1] ```javascript class Car { constructor(brand) { this.carname = brand; } get cnam() { return this.carname; } set cnam(x) { this.carname = x; } } const myCar = new Car("Ford"); document.getElementById("demo").innerHTML = myCar.cnam; ``` **Note:** Even if the getter is a method, you do not use parentheses when you want to get the property value. [S1] Using an underscore backing field so the accessor name matches the property: [S1] ```javascript class Car { constructor(brand) { this._carname = brand; } get carname() { return this._carname; } set carname(x) { this._carname = x; } } const myCar = new Car("Ford"); document.getElementById("demo").innerHTML = myCar.carname; ``` Using the setter to change the value: [S1] ```javascript class Car { constructor(brand) { this._carname = brand; } get carname() { return this._carname; } set carname(x) { this._carname = x; } } const myCar = new Car("Ford"); myCar.carname = "Volvo"; document.getElementById("demo").innerHTML = myCar.carname; ``` **Hoisting** Class declarations are not hoisted; classes must be declared before use: [S1] ```javascript //You cannot use the class yet. //myCar = new Car("Ford") will raise an error. class Car { constructor(brand) { this.carname = brand; } } //Now you can use the class: const myCar = new Car("Ford") ``` For other declarations, like functions, you will NOT get an error when you try to use it before it is declared, because the default behavior of JavaScript declarations is hoisting. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The `Car` / `Model` pair is the running applied example for `extends` and `super()`; the `_carname` accessor examples demonstrate getters/setters, and the hoisting snippet shows the declare-before-use requirement. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Subclass with `extends` and `super()` (language: JavaScript): ```javascript class Model extends Car { constructor(brand, mod) { super(brand); this.model = mod; } show() { return this.present() + ', it is a ' + this.model; } } ``` Getter/setter with an underscore backing field: ```javascript get carname() { return this._carname; } set carname(x) { this._carname = x; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Classes]], [[JavaScript Class Static]], [[JavaScript Objects]], [[JavaScript Object Accessors]] - **μ°Έμ‘° λ§₯락:** Builds on JavaScript Classes by adding inheritance (`extends`/`super`), accessors, and hoisting behavior. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Class Inheritance β€” https://www.w3schools.com/js/js_class_inheritance.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Class Inheritance" page (Astra wiki-curation, P-Reinforce v3.1 format).