//Define the object specific properties inside the constructor
function Human(name, age){
this.name = name,
this.age = age,
this.friends = ["Ali", "Shehzad"]
}
//Define the shared properties and methods using the prototype
Human.prototype.sayName = function(){
console.log(this.name);
}
//Create two objects using the Human constructor function
var person1 = new Human("Mohtashim", 31);
var person2 = new Human("Fasih", 40);
//Lets check if person1 and person2 have points to the same instance of the sayName function
console.log(person1.sayName === person2.sayName) // true
//Let's modify friends property and check
person1.friends.push("Fasih");
console.log(person1.friends)// Output: "Ali, Shehzad, Fasih"
console.log(person2.friends)//Output: "Ali, Shehzad"
// constructor function
function Person () {
this.name = 'John',
this.age = 23
}
// creating objects
const person1 = new Person();
const person2 = new Person();
// adding a method to the constructor function
Person.prototype.greet = function() {
console.log('hello' + ' ' + this.name);
}
person1.greet(); // hello John
person2.greet(); // hello John