Skip to content

'this' reference from static method should be possible #6331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
agu-z opened this issue Jan 3, 2016 · 4 comments
Closed

'this' reference from static method should be possible #6331

agu-z opened this issue Jan 3, 2016 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@agu-z
Copy link

agu-z commented Jan 3, 2016

In JavaScript I am able to reference this from object methods, in order to access properties from the same object that stores my method, for example the prototype of the object.

Suppose I'm writing an ORM and I have a class Model and I want to implement an static method where that does a query in the DB for the corresponding table to that Model. I would write it like this:

class Model {
  public static where(options: any) {
     doWhereQuery(Reflect.getMetadata('table', this.prototype));
  }
}

 // Suppose that I have a decorator that sets the 'table' key in this class metadata
@table('users')
class User extends Model { }

User.where({ name: 'John', surname: 'Doe' });

But when I do so I get: error TS2334: 'this' cannot be referenced in a static property initializer.

I understand that maybe this is not the correct keyword to reference the "class", but that is the way it works in JavaScript and I think that this should be possible Otherwise, there is no trivial way to implement that functionality.

@agu-z agu-z changed the title this reference from static method should be possible 'this' reference from static method should be possible Jan 3, 2016
@RyanCavanaugh
Copy link
Member

It is legal to reference this from a static method. The sample you posted does not produce the error given. Can you clarify?

@agu-z
Copy link
Author

agu-z commented Jan 6, 2016

Oh sorry, you are right. But it produces that error if you do it from an static arrow function and I guessed it would happen from a static method too. Anyway, that is incorrect, isn't it?

class Model {
  public static where = (options: any) => doWhereQuery(Reflect.getMetadata('table', this.prototype));
}

'this' cannot be referenced in a static property initializer.at line 2 col 85

@RyanCavanaugh
Copy link
Member

What's the intent with writing it as an arrow function and referencing this ?

If you want this to always refer to Model, then just write Model instead of this.

If you want this to refer to the class that it's invoked on but not require a method-style invocation (i.e. allow an unbound invocation var x = MyModel.where; x();), that's impossible because derived classes don't get a newly-closed-over own property for where, so there's no way for them to capture this. Trying to write out what the emit would look like in this case is an informative exercise.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Jan 7, 2016
@agu-z
Copy link
Author

agu-z commented Jan 8, 2016

I forgot that arrow functions doesn't have own this, so this issue should be closed. Sorry for my ignorance.

@mhegazy mhegazy closed this as completed Jan 8, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants