we can see
function generate() {
this.a = 'hello';
}
obj = new generate();
so, my question is, how to understand ‘this.’ phenomenon?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, functions are objects.
But there is more to it than that in your sample code. If you call a function with the
newkeyword JavaScript creates a new object for you and then uses the function as the constructor: within the functionthisreferences the newly created object, and the new object will be returned – but this new object is not the function itself: to oversimplify, if your function isgenerate()then the new object is based ongenerate.prototype, which by default is an empty object.Further reading: https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript