Skip to content
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

Call static method without repeating the class name #7673

Closed
yvbeek opened this issue Mar 24, 2016 · 9 comments
Closed

Call static method without repeating the class name #7673

yvbeek opened this issue Mar 24, 2016 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@yvbeek
Copy link

yvbeek commented Mar 24, 2016

When I create a static method, the only way to call it seems to be through the actual class name: MyClass.staticMethod. It would be great if there was a way to call the static method without repeating the class name, it would be less error prone when you later decide to rename the class.

A few suggestions:
self.class.staticMethod or
self.type.staticMethod or
Self.staticMethod

In other languages you can do it like this:

  • PHP: $this->staticMethod(); or $this::staticMethod();
  • Python: self.__class__.staticMethod()
  • Ruby: self.class.staticMethod
  • Objective-C: [[self class] staticMethod]
  • Swift: self.dynamicType.staticMethod()
@jpleclerc
Copy link

A good IDE should be able to rename/refactor your class without any problem, especially if the refactor is in the same class.

@yvbeek
Copy link
Author

yvbeek commented Mar 25, 2016

You can walk to most places instead of driving there, that is not really a reason for not using a car.

@DanielRosenwasser
Copy link
Member

You should be able to write this.staticMethod in a static context of the same class.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Mar 25, 2016
@SBD580
Copy link

SBD580 commented Dec 22, 2016

+1

Not to mention Java where you can call a static method just by it's name (from within the same class of course).

This will make the code much more readable.

@bloadvenro
Copy link

bloadvenro commented Mar 28, 2017

@DanielRosenwasser it is usual for me to call static methods/props from instance methods, but this points to the instance side. this.constructor._property says "Property '_property' does not exist on type 'Function'." Is it difficult to implement reference to static side of the class?

@invasioncredible
Copy link

@bloadvenro +1
Had a problem with this today.

@eugenecherkassky
Copy link

+1

@PrasannaaSanthanam
Copy link

PrasannaaSanthanam commented Mar 2, 2018

+1
This would be a really nice to have feature when you are dealing with really long classNames and does not have the option of changing the class name, especially when you are in a ternary along with x max-line-length

@negora
Copy link

negora commented May 8, 2018

In my opinion, this feature is really important to improve the readability of the code. Although the ability to refactor the code more easily is also welcomed, of course.

When you have to add a class name before a method call, a statement that usually takes 1 line, takes 2 (or even more). Specially if the names of your class, method or arguments (or all them) are not trivial, and you've to restrict the line length to 80 characters.

With regard to the implementation of this feature, I would love to be able to omit the class name entirely, as in Java. But I guess that it could lead to confusion in JavaScript; because one wouldn't know if the code is calling a static method or an independent function.

But that already may happen with instance methods, if you set the option noImplicitThis to false. Doesn't it? For that cases, I guess that editors and IDEs should allow to use a different colour for the calls to functions.

I also like the idea of the self keyword. But I wonder if it would cause problems with code that already uses that word. I guess that the TS developers could create an option, called referencesToSelfStatic or refsToSelfStatic, with the following possible values:

  • explicit: only allows the use of the class name.
  • self: allows the use of the class name or the self keyword (for a given statement).
  • implicit: allows the use of the class name or the self keyword, or their omission (for a given statement).

These values could also be named as: className, className+self, className+self+implicit.

This way, we all could choose the option that better suited our needs.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

9 participants