I've ready quite a bit of blog post and stackoverflow questions but can't get good understanding regarding this.
Why is it necessary to set the prototype constructor?
Trying to understand the difference between prototype and constructor in JavaScript
Constructors in JavaScript objects
JavaScript inheritance and the constructor property
Understanding the difference between Object.create() and new SomeFunction()
Question:
function person(fname) {
this.fname = fname;
}
let obj1 = new person('first'); //works well
console.log(obj1);
let obj2 = new person.prototype.constructor('second'); //works well
console.log(obj2);
//now the query
console.log(person.constructor); //1. it prints function Function() { [native code] } what does it mean
let obj3 = new person.constructor('third') // 2. why this doesn't work ?
console.log(obj3); //
now as we know that a every function in javascript has a property call prototype
which is an object
which has a property called constructor
which points back to the function on which prototype object is a property. - correct me if I'm wrong with this
so according to above statement person.prototype.constructor
=== person
(both are the same)
Query: 1 person.constructor prints function Function() { [native code] }
what does it means, please explain in detail.
Query: 2 why this new person.constructor('randon')
doesn't work ?
Query: 3 what exactly is person.constructor
and how it's different from person.prototype.constructor
?
Aucun commentaire:
Enregistrer un commentaire