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.
<
any function in JavaScript can act as a constructor when the function is invoked with new operator.
Now, what a constructor does ? it creates/instantiate an object from the constructor function. like its shown in the below image.
LINK , it explain the fundamentals very clearly.
what is this
?
when this constructor function is invoked with new, this
points to the new object created at that invocation. and in that object we set firtName
and lastName
(it is the initialization of the new object created).
Now when we add methods to the prototype of the constructor , that is being shared between all the objects created using the constructor function(picture explains it lit bit more)
and regarding your last query "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"
any function in JavaScript can act as a constructor when the function is invoked with new operator, and its not the way that blog says.
so please stop reading that blog, the link i provided is a very good starting point