prosh0t
asked on
defining a class method in javascript
Hello,
I've found two ways to define class methods in javascript, but I don't know which is correct. What's the difference (if any) between the following 2 examples? Also, if I inherited from Foo, how would I call the function 'AddX' in each case?
function Foo()
{
this.x = 1;
}
Foo.prototype.AddX = function(y) // Define Method
{
this.x += y;
}
obj = new Foo;
obj.AddX(5); // Call Method
and the following:
function Foo()
{
this.x = 1;
this.AddX = function(y) // Define Method
{
this.x += y;
}
}
obj = new Foo;
obj.AddX(5); // Call Method
The only difference I can think of is if another class inherited from Foo, and then needed to call AddX. Would the calls be the same? I'm not sure how to do that, does someone know how I could do that for each case?
Thanks
I've found two ways to define class methods in javascript, but I don't know which is correct. What's the difference (if any) between the following 2 examples? Also, if I inherited from Foo, how would I call the function 'AddX' in each case?
function Foo()
{
this.x = 1;
}
Foo.prototype.AddX = function(y) // Define Method
{
this.x += y;
}
obj = new Foo;
obj.AddX(5); // Call Method
and the following:
function Foo()
{
this.x = 1;
this.AddX = function(y) // Define Method
{
this.x += y;
}
}
obj = new Foo;
obj.AddX(5); // Call Method
The only difference I can think of is if another class inherited from Foo, and then needed to call AddX. Would the calls be the same? I'm not sure how to do that, does someone know how I could do that for each case?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>So is 'prototype' basically just a property that every object has?
Yes
>>And when creating classes, in general is it safe to say we should always assign methods to each objects prototype,
Not sure what is "safe" to you. In terms of conserving memory resources, you are better off assigning the methods to the prototype. That way every instance of the object "shares" that method.
>> but properties (specific to each object) we should not assign to a prototype, but instead declare as in method 2
Notice that here you said "properties", not "methods". Properties are meant to be unique per object. So using the first approach will in general be more efficient memory wise
If you had said "methods", then the question is, what is that method doing that is different on a per object instance? If the answer is nothing, then you should not have a method per object instance. Meaning you should be using method 1 above.
Yes
>>And when creating classes, in general is it safe to say we should always assign methods to each objects prototype,
Not sure what is "safe" to you. In terms of conserving memory resources, you are better off assigning the methods to the prototype. That way every instance of the object "shares" that method.
>> but properties (specific to each object) we should not assign to a prototype, but instead declare as in method 2
Notice that here you said "properties", not "methods". Properties are meant to be unique per object. So using the first approach will in general be more efficient memory wise
If you had said "methods", then the question is, what is that method doing that is different on a per object instance? If the answer is nothing, then you should not have a method per object instance. Meaning you should be using method 1 above.
ASKER
thanks!
ASKER
So is 'prototype' basically just a property that every object has?
And when creating classes, in general is it safe to say we should always assign methods to each objects prototype, but properties (specific to each object) we should not assign to a prototype, but instead declare as in method 2?