what is instance object in javascript

In JavaScript, an instance object is an object that is created using a constructor function. The constructor function serves as a blueprint to create multiple instances of objects with similar properties and methods.

Here's an example to illustrate how to create an instance object in JavaScript:

index.tsx
// Constructor Function
function Person(name, age) {
    this.name = name;
    this.age = age;
}

// Creating an Instance Object
let person1 = new Person('Alice', 30);
let person2 = new Person('Bob', 25);

console.log(person1); // Output: Person { name: 'Alice', age: 30 }
console.log(person2); // Output: Person { name: 'Bob', age: 25 }
338 chars
13 lines

In this example, Person is a constructor function used to create person objects. The person1 and person2 objects are instances of the Person constructor function with their specific name and age properties.

related categories

gistlibby LogSnag