Is there any difference between these two:
var test1 = function () {
this.method1 = function() {}
}
and
var test2 = function() {};
test2.method1 = function() {};
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.
The first snippet takes
thisobject, whatever it is, and assigns a function to its slot (field) namedmethod1.thiscan represent different objects, depending upon howtest1is called:test1()—thiswill bewindownew test1()—thisrefers to the object being createdcallorapply—test1.apply(someObject)—thisrefers to the argumentThe second snippet takes the object
test2and assigns a function to its slot namedmethod1.