Matteo Pagliazzi
Matteo Pagliazzi

Reputation: 5260

Javascript "this" in static methods

I have a code like that:

User = function(){}

User.a = function(){
  return "try";    
}

User.b = function(){

}

​ From User.b() I can call User.a() using:

User.b = function(){
    return User.a();
    }

but not using this since it's not an instance of user (User.a() and User.b() are something like "static methods").

What i want to do is to be able to call User.a() from User.b() without knowing which is the main function, in this case User.

Something like this to be used in static methods.

Upvotes: 8

Views: 25727

Answers (5)

AsukaMinato
AsukaMinato

Reputation: 1432

Now js has class and static keywords, though they are just syntax sugar.

class User {
    static a() {
        return "try";
    }
    static b() {}
}
console.log(User.a())

Upvotes: 0

Esailija
Esailija

Reputation: 140210

In reality there is no methods or static methods in js, there's just functions that are assigned to object properties (functions being objects as well) and they all work the same way. Since you are calling it like User.b(), this will be User for the call.

User.b = function() {
    return this.a();
}

Upvotes: 16

Guffa
Guffa

Reputation: 700192

The only thing that determines the context of the function is how you call it.

If you call it using a plain identifier (a function name, a variable or a property), the context will be the global window object:

someFunction();

If you call it using a period to access an object member, the context will be the object:

someObject.someFunction();

If you copy a member from an object to a variable, there is no connection to the object any more, and it will be called with window as context:

var x = someObject.someFunction;
x();

If you assign a function as a property to an object, and call it using the object, the context will be the object:

someObject.x = someFunction;
someObject.x();

For your specific case, User is a function, which also is an object.

If you call the function using User.b, its context will be the User object, which happens to be a function in this case. From within the function you can still use this to access the context:

User.b = function(){
  return this.a();
}

Upvotes: 8

Mark Reed
Mark Reed

Reputation: 95242

Well, functions exist independently of their container objects; they're just values. So if you're not calling them as methods on an object, they inherit whatever this is in the calling context. In that case, expecting them to know about their container would be the same as assigning User.x the value 1 and then expecting the number 1 to somehow know about User.

However, when you call User.a() or User.b(), you are in fact calling them as methods - methods of the (function object) User. So this will be the same as User, and b can just call this.a() and you should be good to go.

Upvotes: 0

Pablo Mescher
Pablo Mescher

Reputation: 27437

You don't have normal inheritance in javascript. I guess you are trying to do something like this:

User = function(){
    this.a= function(){
         return 'try';
    }
    this.b= function(){
        return this.a();
    }
}

This way User becomes a constructor. Each new instance of User will have acces to these methods. So if you want to create a new instance of the User class, you can use the new keyword:

var client= new User()

and then you'll have access to all the methods from user using client

client.b() //returns 'try'

Upvotes: 0

Related Questions