
Constructor in JavaScript
A constructor function is a special function used to create many objects with the same structure. Curly braces represent an object. Create many Objects easily. Each Key-value pair is called Property Instead of writing many objects manually, we create a blueprint name constructor functions with an upper-case first letter. constructor() Special method inside a class Runs automatically when object is created function Person ( name , age ) { Explanation function Person(name, age) function → keyword to define a function Person → constructor name name, age → parameters Understanding this this.name = name; this=>refers to the current object being created this.name=>creates a property called name inside the object = name(assigns the value received from the parameter) Example : { name : "Priya" } this.age = age; creates an age property stores the value passed to the constructor object becomes: { name : "Priya", age : 21 } Steps: Creates an empty object { } Sets this to point to that object Call
Continue reading on Dev.to
Opens in a new tab



