Skip to content Skip to sidebar Skip to footer

Static Method Instead Of Prototype Method Javascript

When instance function are called in a class each instance of object get own copy of function but in prototype method and static method no copy is created , they belong to class, s

Solution 1:

In order to use a prototype/instance method, you need to either have an instance of an object or specifically access the type's .prototype. For methods that don't require an instance, a static method provides simpler syntax. Think of the String.fromCharCode() method as an example. It wouldn't make sense to say:

let str = "dummy string".fromCharCode(127);

The extra string instance there is just a distraction from what you're really trying to do:

let str = String.fromCharCode(127);

This applies good programming practices of reduced coupling (not requiring an instance in order to invoke a method that doesn't need it) and information hiding (by not exposing a method on instances of objects which doesn't pertain to those specific objects).


Solution 2:

A static method does not exist on instances. Prototype methods do. So, if you want to call someArr.filter(x => x > 5) that would be an instance method that works on the given array.

An example of astatic method is Array.isArray(someArr). It makes very little sense to make the static method an instance method because you'd need an instance before calling it. That would lead to code like someArr.isArray(someArr) which is illogical - you need an array to check if something is an array. And that can very easily be fail spectacularly if someArr is not in fact an array:

const someArr = { 
  isArray() { return true; },
  filter()  { return "I am not an array"; },
};


console.log(someArr.isArray(someArr));
console.log(someArr.filter(x => x > 5));

Yes, that example is indeed highly illogical in order to highlight why it is weird. Assuming .isArray() was an instance method, you could create a new array in order to use it to call [].isArray(someArr). But that method does not require any instance data. The object created exists only to give you access to the method and is discarded immediately afterwards. That design is still not sensible.


Solution 3:

Both static methods and prototype methods exist independent from any instances. The difference is that a prototype method expects to be called on an instance, i.e. to have an instance passed as the this argument, whereas the static method does not require an instance and expects none.

Even without placing them anywhere on the class, we can see this distinction in the following example:

function myStaticMethod(arg) {
    console.log('Doing something with '+arg);
}
function myMethod(arg) {
    console.log('Doing something with '+this+' and '+arg);
}
const myInstance = new MyClass();

myStaticMethod('value');
myMethod.call(myInstance, 'value');

Now the .call() syntax is not very ergonomic, we prefer myInstance.myMethod('value'), so that is why we place the method on the prototype object of the class, having it inherited by all instances.

For static methods, this is not necessary. They don't need an instance, we don't want to call them on an instance, we want to call them as MyClass.myStaticMethod('value') so that is where we place them. We could put them on the prototype as well, but that would lead to confusion (myInstance.myStaticMethod()), name collisions, and unncessarily long invocations (MyClass.prototype.myStaticMethod()). It's imaginable to write new MyClass().myStaticMethod(), but there you would unnecessarily create an instance that is not required (and it might not even be possible to create).


Post a Comment for "Static Method Instead Of Prototype Method Javascript"