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

[Proposal] Independent private field #12

Closed
Aqours opened this issue Aug 29, 2017 · 4 comments
Closed

[Proposal] Independent private field #12

Aqours opened this issue Aug 29, 2017 · 4 comments

Comments

@Aqours
Copy link

Aqours commented Aug 29, 2017

e.g: code

const logName = function () {
    console.log(private.name);
};

class A {

    name = 'public name';
    private name = 'private name';
    // private value = 'private value'; [1]

    constructor() {
        private.value = 'private value'; // like [1]
        private.logName(); // `private.` like `this.`, but cannot access by instance.
        logName.call(this); // 'private name'
    }

    private logName() {
        console.log(private.name);
    }

}

const a = new A();
console.log(a.name); // 'public name'
console.log(a.logName); // undefined
console.log(A.prototype.logName); // undefined

A.prototype.logName = function () {
    console.log(`${this.name} + ${private.name}`);
};
a.logName(); // 'public name + private name'
@littledan
Copy link
Member

I don't know what you're getting at here.

@claudepache
Copy link

@Aqours If you can use the word private for accessing the private state of an instance of A outside of the lexical scope delimited by class A { /* ... */ }, ... it means that it is no longer private state.

@Aqours
Copy link
Author

Aqours commented Aug 30, 2017

@claudepache We can modify the prototype of a class dynamically. I think private variable can be accessed by method in prototype, but cannot be accessed directly by property.

@littledan
Copy link
Member

As @claudepache explained above, syntax based on private and . is not feasible for JavaScript.

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

3 participants