Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a method from a class without instantiating it?

Tags:

javascript

oop

If you look at a framework like cocos2d-x, for example:

cc.Sprite.extend();

Sprite here is a class; how can I call one of its methods without instantiating it with the new keyword?

For example, if I have this class:

var superClass = function(){
    
    this.init = function(){
        console.log("new class constructed !")
    };

};

To call init, I must do this:

obj = new superClass();
obj.init();

But how can I call the function without needing to instantiate the class?

like image 625
Sky Walker Avatar asked May 24 '15 09:05

Sky Walker


People also ask

Which methods can be used without instantiating an object?

Static Method Static methods are the methods in Java that can be called without creating an object of class.

Can static methods be called without instantiating the class first?

In the same way that a static variable is associated with the class as a whole, so is a static method. In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object.

How do you call a method in Java without creating an object?

This is not possible in java. You cannot call an method without an object.

What type of methods can be called without instantiating an object first?

In a Web API, a static method is one which is defined by an interface but can be called without instantiating an object of that type first.


2 Answers

ES6 and newer:

Use the static keyword. It's supported by most modern browsers.

class SuperClass {
   static init() {
     console.log("new class constructed !")
   }
}

SuperClass.init();

Older browsers:

There are different ways to achieve that. One option is to use an object literal.

Object literal:

var superClass = {
    init: function(){
        console.log("new class constructed !")
    }
};

superClass.init();

https://jsfiddle.net/ckgmb9fk/

However, you can still define an object using a function constructor and then add a static method to the "class". For example, you can do this:

var SuperClass = function(){};

SuperClass.prototype.methodA = function() {
    console.log("methodA");
};

SuperClass.init = function() {
    console.log("Init function");
}

SuperClass.init();

var sc = new SuperClass();
sc.methodA();

Note that the method init won't be available in the SuperClass instance object because it is not in the object prototype.

https://jsfiddle.net/mLxoh1qj/

Extending prototype:

var SuperClass = function(){};

  SuperClass.prototype.init = function() {
      console.log("Init");
  };

SuperClass.prototype.init();

var sc = new SuperClass();
sc.init();

https://jsfiddle.net/0ayct1ry/

This solution is useful if you want to access the function init both from the SuperClass prototype and from an object instance.

like image 51
Giuseppe Pes Avatar answered Oct 03 '22 03:10

Giuseppe Pes


If you assign the method this way, you will not be able to call it without instanciating the class, but you can achieve this by extending the prototype:

var superClass = function(){
};

superClass.prototype.init = function(){
    console.log("new class constructed !")
};

var obj = new superClass();
obj.init();
superClass.prototype.init();
like image 30
Daniel Perez Avatar answered Oct 03 '22 04:10

Daniel Perez