Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript Create Multiple Objects with Constructor Function

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23,

     this.greet = function () {
        console.log('hello');
    }
}

// create objects
const person1 = new Person();
const person2 = new Person();

// access properties
console.log(person1.name);  // John
console.log(person2.name);  // John
 
PREVIOUS NEXT
Tagged: #JavaScript #Create #Multiple #Objects #Constructor #Function
ADD COMMENT
Topic
Name
2+8 =