9

So I am new to javascript (or any programming language) and as I progress I hear new keywords.

I was going through this question on Stackoverflow: How to access a method from a class from another class?

Where the reply or answerer wrote something like this..

if it's a static method (doesn't use any instance data), then declare it as a static method and you can directly call it.

If it's an instance method, then you would typically create an object of type one and then call the method on that object (usually in the constructor).

Can someone explain the difference Between Static and Instance Method with examples? How do we call static and instance method in javascript?

  • Possible duplicate of [Difference between Static methods and Instance methods](https://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods) – mohammad javad ahmadi May 09 '18 at 11:12
  • see [link](https://github.com/30-seconds/30_seconds_of_knowledge/blob/7cfd4d933422191f94216b16242538a1ea144440/src/assets/snippets/interview/static-vs-instance-method.md) – mojtaba ramezani Oct 10 '19 at 05:02

3 Answers3

14

This answer is not for Javascript, but OOP in general.

Imagine, you have a class Person. An instance of that class could be daniel.

Instance method

You could call a method on daniel, e.g: daniel.talk(), so daniel starts talking...

Static method

You could call an static method on class Person, instead of on a concrete instance, for example: Person.getPeopleFromNewYork(). Note that getPeopleFromNewYork is not related to any instance of Person but to the class itself.

(Tipically, a method like getPeopleFromNewYork() would belong to some kind of repository, but it's just for the example.)

Another illustrative examples for understand static methods are Math.sum(2, 5) or Random.randomInt()

Héctor
  • 24,444
  • 35
  • 132
  • 243
3

Javascript has two type of property and method. Static and instance both type of property and method can you use.

First of all you need to know how object can defined in javascript .. Two major way is using {} or using constructor. in {} every property and method is static and you can not create an instance of that , such as :

<script> 

var person = {name:"Chayon Shaah",age:"30",say:function(){return "Hello "+this.name}} 
alert(person.say()); // will allert "Hello Chayon Shaah"

</script>

just see the code , here you need to call any property or method by object reference and can't creat any instance of this person object using new keyword

and now see the constructor , how it can create an instance :

<script>
function Person(){
 this.name="Chayon Shaah",
 this.age = 30,
 this.say = function(){
 return "Hello "+this.name;
 }

}

var obj = new Person();
alert(obj.say()); // will alert "Hello Chayon Shaah"

</script>

Here all of Person object properties and method can be accessed by the "obj" whice is an instance of Person object.

Person object directly can not access them , only its any instance can access them . On the other hand you can use static property or method for Person object whic can only acess by its own not by its any instance.

let's add some more instance property and static property in Person object ...

<script>
function Person(){
 this.name="Chayon Shaah",
 this.age = 30,
 this.say = function(){
 return "Hello "+this.name;
 }

}

var obj = new Person();
alert(obj.say());

Person.prototype.city = "Mymensingh"; // only can be used by all instances of Person object (non static property).


  alert(obj.city); // Will alert "Mymensing" (Calling via instance of Person)

Person.location = "Bangladesh"; //This is a static property which can not be accessed by any instance of Person object, Only accessed by Person Object.
 `alert(Person.location); // Will alert "Bangladesh" (Calling directly via` Person object)
</script>

use "prototype" keyword for making any instance method or property with the main object name, If you want to make static property and method then just use the name without use "prototype" keyword like the example .

I Think it will help you ...

Chayon Shaah
  • 109
  • 1
  • 4
1

For Javascript specific answer please visit this link ( I found it by googling ) It seems good.

But regardless of which object oriented programming language you've chosen; generally you'd use a static method for functionalities which don't need to know other field/property status of the class. For example, converting one unit of length to another doesn't need to know what are other properties of the object. On the other hand, let's assume that we have a class Customer with properties first name and last name. Now if you needed to derive full name by concatenating first and last name you'd could create a method on class GetFullName() which doesn't take parameters and does the job for you. So on object of type Customer you could use object.GetFullName() without any parameters to get the full name. Of course, you could write a static method for the same purpose but then you'd have to pass parameters to the method. For a method which depends on a large number of parameters, it would be cumbersome.

sbp
  • 913
  • 8
  • 19