Hey i'm just looking for a javascript explanation to explain why 'constructor called' is only called once as opposed to 5 times?
const Dog = function(name, age){
this.name = name;
this.age = age;
console.log('constructor called');
}
const obj = {
dog: new Dog('roofus', 20)
}
const main = ()=>{
for(let i = 0; i < 5; i++){
console.log(obj.dog)
}
}
main();
'constructor called'
Dog { name: 'roofus', age: 20 }
Dog { name: 'roofus', age: 20 }
Dog { name: 'roofus', age: 20 }
Dog { name: 'roofus', age: 20 }
Dog { name: 'roofus', age: 20 }
Dogis called 5 times? The instance that you've created is logged 5 times, butnew Dog()is only called once, when you defineobj.