Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Access from this should be supported #62

Open
earonesty opened this issue Jul 9, 2020 · 5 comments
Open

Access from this should be supported #62

earonesty opened this issue Jul 9, 2020 · 5 comments

Comments

@earonesty
Copy link

earonesty commented Jul 9, 2020

Allowing statics and functions of the same name is currently how the babel plugin works. I suspect this is confusing. IE:

This should work.

class Foo {
   static flag = 4
   logFlag() {
       console.log(this.flag)
   }
}

This should be a syntax error:

class Foo {
   static flag = 4
   flag() {
       console.log(Foo.flag)
   }
}
@ljharb
Copy link
Member

ljharb commented Jul 9, 2020

In your first example, there's Foo.flag === 4 and Foo.prototype.logFlag, which would log undefined. What's not working for you in that one?

In the second example, since there's no conflict between Foo.flag and Foo.prototype.flag, why should that be a syntax error? You can already do this:

class Foo {
  static get flag() {}
  flag() {}
}

so it'd be very confusing imo if your second example errored.

@invader-viim
Copy link

Allowing statics and functions of the same name is currently how the babel plugin works. I suspect this is confusing. IE:

This should work.

class Foo {
   static flag = 4
   logFlag() {
       console.log(this.flag)
   }
}

This should be a syntax error:

class Foo {
   static flag = 4
   flag() {
       console.log(Foo.flag)
   }
}

if it's a "static" property it should never be accessible from "this" i would think, the idea of it being static means those properties and methods are not available within the instantiated class thus no "this".

@caiolima
Copy link
Contributor

Hi @earonesty.I didn't understand the problem this issue reports. Do you mind explaining a bit further?

@wentout
Copy link

wentout commented Apr 19, 2021

Let assume we have simple function.
We can join a field to the reference, and that mean static from the early beginning of JS.

var Foo = function () {
    console.log(this.flag === undefined); // true
    console.log(Foo.flag === 4); // true
};
Foo.flag = 4;

So, is this OK if we will change this behavior for classes?
If so then we will be able to change flag field from any of inside of the constructor.
And also we will be able to change it from super pointer.
And, what if we use Proxy for something nested in Prototype Chain, how Reflect will work then?

The way it should retrieve an access is an explicit access through .constructor property:

class Foo {
    static flag = 4
    constructor() {
        // explicit access
        // no hidden behavior
        // thinking of code review
       console.log(this.constructor.flag);
   }
}

My hope this will never see static field directly.

@ljharb
Copy link
Member

ljharb commented Apr 19, 2021

@wentout That example will work, as would the better practice of not relying on .constructor and hardcoding Foo.flag. Use of Proxy shouldn't change anything.

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

No branches or pull requests

5 participants