Snippet1:
var box = function() {};
box.prototype.open = function {
};
Snippet2:
var box = function() {
this.open = function() {
};
}
Any difference between those two, which one is better?
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.
Shall we assume that
boxis a constructor, so you’re doingnew box()?If so…
The first version will share the
openfunction among all objects created from theboxconstructor.The second will generate a new function object for every object created from the
boxconstructor.As such, the first will be more memory efficient than the second.
First version:
Second version: