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

Expose statics on instances #14

Open
rektide opened this issue Mar 7, 2017 · 4 comments
Open

Expose statics on instances #14

rektide opened this issue Mar 7, 2017 · 4 comments

Comments

@rektide
Copy link

rektide commented Mar 7, 2017

I'm curious why this.#helper() would be disallowed? This seems to be a very very common language pattern, but I've never understood a good reason for it.

I understand that it's not the instance per-se that has the field, but it's class does, and the # implicitly says to me- look at this instance's class for this field.

There are a number of very straightforward and useful places where having static methods virtualized seems like it'd be potentially very handy. A really simple example of what this could enable:

class Human{
  static move(){ console.log("walk walk walk")
  constructor(){}
  loop(){ this.#move() }
}
class Driver extends Human{
  static move(){ console.log("drive drive drive") }
}
class Racer extends Driver{
  static move(){ console.log("race race race") }
}

It seems intuitively obvious and simple to a laymen like me that I'd want to be able to ask for whatever the current class's static is. Time and time again I run into languages where this is impossible or requires extensive and painful reflection steps to accomplish. I can understand the conceptual argumentation that this doesn't have a static field, but especially if we have a symbol designating that the field is static- # it seems like there's no chance for ambiguity and we can should and ought allow this straightforward ask- give me the static field for whatever my class is.

In EcmaScript, not having this would imply having to write userland code to walk the prototype chain, looking at constructors. This seems suboptimal and painful from a usability and performance standpoint both.

I'd like to see statics exposed on instances, especially given that there is a # indicator in front of the field indicating very clearly the user's intent to look at statics. Please allow this.#helper().

@allenwb
Copy link
Contributor

allenwb commented Mar 20, 2017

The # prefix doesn't mean "static". If is something completely different, a non-property "field" that can only be referenced from within the body of the class that defines it.

What you are asking for seems to be what some language call "protected" fields (or properties).

@rektide
Copy link
Author

rektide commented Mar 24, 2017

I must have been conflating different topics.

I'm not very interested in private fields.

My real interest is in getting a this.myStatic, in having static imply singleton accessible both from on the constructor and from the instance. It feels like more of a stretch ask, but semantically I've always found it be-deviling that no language makes it easy to ask for a static by name for the current instance.

@ljharb
Copy link

ljharb commented Mar 24, 2017

Do you mean you want a syntactic way to do this.constructor.foo, like static.foo?

@allenwb
Copy link
Contributor

allenwb commented Mar 24, 2017

or something like what is shown in #16 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants