Calling a method from a Javascript class (OOP)

Asked

Viewed 3,244 times

5

Good staff would like to know if it is possible to access a method of a class(javascript) without knowing if the variable by instance it.

function SDK(dynamicUrl) {
    var data = 'DATA';
    var atual = null;
    var counter = 0;
    this.onComplete = function (){ }
    this.onError = function () { }
    this.start = function ( ) {
        alert('HUE BR')
    }
}
//Tendando acessar o metodo
SDK.start();

*I know I could access it by creating a variable and instantiating the class in question, but by a limitation of the project in question this is not possible T.T

var sdk = new SDK('HUE BR');
sdk.start();

URL of the script: http://jsfiddle.net/8twxajLh/

  • 1

    Why don’t you use it directly SDK.start = function() {...}. In this case it is creating a function within a function (which is an object).

  • So this way I can not because I do not have access to the data of the instance in question but even so thank you.

  • Directly from SDK can’t. Why can’t you use new?

  • So face is a limitation of the project( I am responsible for the script ), because in my case I am not responsible for creating the new SDK(), ie I have no access to variable only have access to class T.T; But Valew msm so if there is no way msm I will try something else

  • If you have a reference to the class, even if it is not the class itself, you can still create an instance of it - see http://jsfiddle.net/d3a5ozvofor example/

3 answers

8


Reading the answer from Rui Pimentel, it occurred to me that you can create an instance within the function itself, if it is called without new. The practical consequence of this is that it does not matter to call with or without the new:

function SDK(dynamicUrl) {
    if(!(this instanceof SDK)) {
       return new SDK(dynamicUrl);
    }
    var data = 'DATA BITCH';
    var atual = null;
    var counter = 0;
    this.onComplete = function (){ }
    this.onError = function () { }
    this.start = function ( ) {
        alert('HUE BR')
    }
}
SDK().start();

http://jsfiddle.net/164geejk/

I’m not sure if this solves the problem given its limitations, but here’s the tip.

  • Puts, guy worked !!! thank you very much, my cabaçada was unaware of instanceof( I must use it often ) Valew !!!

  • @Douglasdossantos chooses the bfavaretto answer then so that other people with the same problem benefit from the solution!

4

No, you cannot access an instance variable without having an instance for it. The function start is only created when creating a class instance SDK. You can use some tricks not to call the new SDK() that behind the scenes would end up calling the "constructor", but this would only add unnecessary complication to your code.

  • Totally agree. Example of artifice: http://jsfiddle.net/qabb81py/

  • Thank you gentlemen it is very clear now, I will find another VALEW solution!

3

If you can add the line

return this;

as the last of the function/class declaration SDK, then it is possible to call the desired function instantiating (var a = new SDK(); a.start();) or not the prototype (SDK().start();).

Edit: as pointed out by @bfavaretto, this method has the side effect of declaring, in the global object (window), the methods (and possibly attributes) of the class that have been declared with this.metodo = function( ... ){ ... };, as soon as the function is executed (SDK().start()), because, in this case, the this of the function shall be the window.

  • Thanks Rui, within the limitation( I am not responsible for the whole code ), it is really possible to call the function without knowing the variable that instantiates the class. I will look for another solution. Thank you very much

  • 1

    I updated my answer after the discussion. The conclusion is that THERE IS an ideal solution to the OP problem, and it was pointed out in @bfavaretto’s reply.

  • 1

    It worked out, Valew !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.