Object-Oriented JavaScript Tip: Creating static methods, instance methods
Custom JavaScript objects can have instance methods (function that are associated with a particular JavaScript object), but like other Object-Oriented languages, they can also have static methods, that is functions that are associated with the JavaScript class that created an object, as opposed to the object itself. This is useful in cases where a function (a.k.a. a method) will not be different in different object instances. Let’s look at an example…
Suppose you created a class to handle simple arithmetic calculations:
function Calculator() { } |
To begin with, an instance method could be added to this class in one of two ways, either inside the constructor or through the class prototype. In this example, one method called multiply will be created, which returns the product of two values multiplied together. First, implemented in the constructor it looks like:
function Calculator() { this.multiply = function(val1 , val2) { return (val1*val2); } } |
Via the class prototype, which is a more readable solution in my opinion, it would look like:
function Calculator() { } Calculator.prototype.multiply = function(val1 , val2) { return (val1*val2); } |
Use of this method would then occur through instances of the Calculator class, like so:
var calc = new Calculator(); alert( calc.multiply(4,3) ); //pop-up alert with product of 4 times 3 |
However, it shouldn’t really be necessary to create an object to use the multiply method, since the method isn’t dependent on the state of the object for its execution. The method can be moved to the class to clean up this code a bit. First the class definition is created, which looks almost identical to the instance method declaration above, with the exception of the prototype keyword being removed:
function Calculator() { } Calculator.multiply = function(val1 , val2) { return (val1*val2); } |
Now the multiply method can be called through the class itself, instead of an instance of the class, like so:
alert( Calculator.multiply(4,3) ); //pop-up alert with product of 4 times 3 |

August 7th, 2009 at 1:51 pm
Thanks for the clear and concise article!
October 5th, 2009 at 9:29 pm
I was looking for a way to create static methods with Dojo and came across this post. Great explanation!
October 20th, 2009 at 10:25 pm
I’m newbie on JavaScript and there’s something that’s not clear to me. It’s about prototyping.
I’m reading “JavaScript, The Definitive Guide” and what I undestand is that methods/properties created using prototype apear has they were part of the instance but they really aren’t. They’re common to all instances from the particular object.
So my question is if they’re considered instance or static.
Thanks,
October 20th, 2009 at 10:44 pm
Hi Victor,
Hmm… I would consider them instance methods, since they are accessible through instances of a class (albeit all instances), whereas a static method is accessible in the class itself.
August 2nd, 2011 at 6:41 pm
how to override instance method “findLoc”
function Person(gender) {
this.name = ‘Generic Name’;
this.gender = gender;
this.active = false;
this.findLoc = function(){
return “Antartica”;
};
var p = ‘i am private variable’;
alert(‘Person instantiated’);
}
// should override??? <— does not work!
Student.prototype.findLoc = function(){
return "America and "+Object.prototype.call(this);
}
August 4th, 2011 at 4:07 am
Hi Boonga,
Just looking over your code it looks like you are defining Person, but then trying to override the function on Student, not Person.
December 16th, 2011 at 9:20 am
Good explanation…thanks!
January 31st, 2012 at 9:59 pm
Thanks!
March 26th, 2012 at 11:02 am
What’s the advantage of static methods compared to a individual Object defined like this:
var utilObject = {
getTranslation: function(text)
{ // translate and return }
}
?
April 24th, 2012 at 9:14 am
Thanks Lakukaa, just what I was looking for.
September 22nd, 2012 at 7:21 am
@Lakukaa
It’s really more about readability. In the example you show (utilObject), you’re attaching a function as the property of an object. In the original example, the Calculator constructor simply serves the same purpose: the multiply function is attached as a property of an entity. Only in that case, the entity is a function (constructor) instead of an object.
The Calculator function serves no other purpose that to act as a namespace for the multiply function. Let’s say you had a JavaScript constructor that provided a set of instance methods and you needed a utility method or a set of constants associated with that ‘class’. The constructor function is a convenient mechanism for namespacing these types of things.
October 6th, 2012 at 9:02 pm
@Lakukaa Because according to object oriented design standards, you should *never* be creating more objects than is necessary. There is much less overhead involved when instantiating a class once, as opposed to instantiating a new object any time you want to execute that object’s function(s).
Using your method, you may not notice a performance impact while running relatively simple scripts, but for blocks of code that could potentially be executed thousands to millions of times, you would be forcing the end-user to set aside memory for thousands to millions of unique objects.