--- id: javascript-object-constructors title: "JavaScript Object Constructors" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Object constructor function", "constructor function", "new keyword", "Person constructor", "prototype property"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "objects", "constructors", "prototype"] raw_sources: ["https://www.w3schools.com/js/js_object_constructors.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Object Constructors]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A constructor function is a reusable "object type" blueprint: call it with `new` to stamp out many objects of the same kind, where `this` becomes each new object. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Constructor functions are object blueprints** β€” when you need many objects of the same type, define a constructor function and create instances with the `new` keyword. [S1] - **Naming convention** β€” it is considered good practice to name constructor functions with an upper-case first letter (e.g. `Person`). [S1] - **`this` inside a constructor** β€” `this` has no value of its own; it becomes the new object being created when the function is invoked with `new`. [S1] - **Prototype changes affect all instances** β€” a property or method added to `Constructor.prototype` is shared by every object created from that constructor, whereas a property added to one object affects only that object. [S1] - **Use literals over built-in constructors** β€” JavaScript has built-in constructors (`new Object()`, `new Array()`, etc.), but the recommended best practice is to use the literal forms instead. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Capitalized constructor + `new`** β€” define `function Person(...) { this.x = ... }` and instantiate with `const p = new Person(...)`. [S1] - **Default property in the constructor** β€” assign a constant property inside the constructor body (`this.nationality = "English";`) so every instance gets it. [S1] - **Per-object vs. shared** β€” `obj.prop = value` adds to one object; `Constructor.prototype.prop = value` adds to all. [S1] - **Method as a property** β€” attach a function expression to `this` (`this.fullName = function() {...}`) to give instances a method. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Object Type (constructor function)** When you want to create many objects of the same type, you can use an object constructor function. It is considered good practice to name constructor functions with an upper-case first letter. The example below defines a `Person` object type: [S1] ```javascript function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } ``` **Creating objects with `new`** Once the object type exists, you create new objects of that type with the `new` keyword: [S1] ```javascript const myFather = new Person("John", "Doe", 50, "blue"); const myMother = new Person("Sally", "Rally", 48, "green"); const mySister = new Person("Anna", "Rally", 18, "green"); const mySelf = new Person("Johnny", "Rally", 22, "green"); ``` **Property Default Values** A value assigned to a property inside the constructor becomes a default value for all objects created from it: [S1] ```javascript function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.nationality = "English"; } ``` **Adding a Property to an Object** Adding a property to a created object is easy, but it is added only to that single object β€” not to other objects of the same type: [S1] ```javascript myFather.nationality = "English"; ``` **Adding a Property via Prototype** Adding a property to the constructor's prototype adds it to all objects created from the constructor: [S1] ```javascript Person.prototype.nationality = "English"; ``` **Constructor Method** A method is a function defined as a property of the object. Methods can be defined inside the constructor: [S1] ```javascript function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.fullName = function() { return this.firstName + " " + this.lastName; }; } ``` **Built-in JavaScript Constructors** JavaScript has built-in constructors for native objects: [S1] ```javascript new Object(); // A new Object object new Array(); // A new Array object new Map(); // A new Map object new Set(); // A new Set object new Date(); // A new Date object new RegExp(); // A new RegExp object new Function(); // A new Function object ``` Best practice: there is no reason to use these built-in constructors. Use the literal equivalents instead β€” `{}` for `new Object()`, `[]` for `new Array()`, `/()/` for `new RegExp()`, and `function (){}` for `new Function()`. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's `Person` examples are the canonical applied use: defining an object type once and creating multiple instances (`myFather`, `myMother`, `mySister`, `mySelf`) from it, then extending them with default properties, prototype properties, and methods. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Define a constructor and instantiate (language: JavaScript): ```javascript function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } const myFather = new Person("John", "Doe", 50, "blue"); ``` Share across all instances via prototype: ```javascript Person.prototype.nationality = "English"; ``` Add a method: ```javascript this.fullName = function() { return this.firstName + " " + this.lastName; }; ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **Constructor vs. object literal** β€” use a constructor function when you need *many* objects of the same type; for a one-off object, a literal `{}` is simpler. [S1] - **Per-object property vs. prototype property** β€” add to the object directly for an instance-specific value; add to the prototype to share across all instances. [S1] - **Built-in constructor vs. literal** β€” prefer literals (`{}`, `[]`, `/()/`, `function(){}`) over `new Object()`, `new Array()`, etc. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Objects]], [[JavaScript Object Properties]], [[JavaScript Object Methods]], [[JavaScript Object Prototypes]] - **μ°Έμ‘° λ§₯락:** Referenced whenever you need a reusable object template instead of a single object literal. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Object Constructors β€” https://www.w3schools.com/js/js_object_constructors.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Object Constructors" page (Astra wiki-curation, P-Reinforce v3.1 format).