In one of my questions, I got the following code as one of the answers. My understanding of the language has come has come a far better now, just have one small question.
var person = function() {
this.firstName = "";
this.lastName = "";
}
person.prototype.showFullName = function () {
console.log(this.firstName + " " + this.lastName);
}
var perObj = new person();
perObj.firstName = "Penelope";
perObj.lastName = "Barrymore";
perObj.showFullName();
Considering the object,
var person = function() {
this.firstName = "";
this.lastName = "";
}
and when I call this object using,
var perObj = new person();
Is this similar to constructor kind of thing?
The moment a code
var perObj = new person();
is invoked will the following two lines automatically get executed?
this.firstName = "";
this.lastName = "";
And also in one of the blogs I was studying if the file name is Samplescript.js
and if a function is written using the same name inside this like var Samplescript=function(){}
, will this function be considered a constructor?
Please clarify me this.
I am not getting any satisfying answer regarding constructor practically even though theoretically things are clear, in this example the way its written makes lot of clear understanding.