3

I am trying to create a class using the class foo {} syntax, and want a private variable inside of it. Right now, I have something like this:

'use strict';
class foo {
  constructor(x1,y1) {
    var x = x1;
    var y = y1;
  }
  get x() {
    return x;
  }
  get y() {
    return y;
  }
}

Whenever I try to access foo.x and foo.y, I get a x is not found error. However, when I tried putting the variable declarations outside of the constructor, it stopped working. I'm also using static methods, so I can't abandon the class syntax without some work. How can I fix this? How can I declare a variable inside of the object, that is global and can't be found outside?

3
  • Have you tried writing this.x = x1; instead of var? Commented Mar 23, 2017 at 20:48
  • 1
    @GabrieleB-David declaring this.x would allow access to x from outside the class. Commented Mar 23, 2017 at 20:49
  • @realseanp you're right. I missed the part where he said "private variable". I guess my elementary school teachers were right all those years ago: read the whole question before answering :P Commented Mar 23, 2017 at 20:52

3 Answers 3

4

Those getters are declared on the prototype, where they cannot access the private (scoped) variables in the constructor. You'll need to use Object.defineProperty for creating instance-specific getters:

class Foo {
  constructor(x, y) {
    Object.defineProperties(this, {
      x: {get() { return x; }, configurable: true},
      y: {get() { return y; }, configurable: true}
    });
  }
}

This is no different than in ES5. Btw, given you don't assign to the variables, you might as well make them non-writable properties.

Sign up to request clarification or add additional context in comments.

2 Comments

How could I make a variable/property that is only writable inside of the instance?
@Wyrme x and y (the parameters) already are exactly such variables
1

Well people are assuming you want to use getters and setters. If that's not the case, this will suffice:

'use strict';
class foo {
  constructor(x1,y1) {
    this.x = x1;
    this.y = y1;
  }
}

var f = new foo(10, 20);

console.log(f.x)
console.log(f.y)

JSFiddle: https://jsfiddle.net/gLxnqfrv/

Comments

0

Taken from es6-features this is the correct syntax for a class and it's getters and setters.

class Rectangle {
    constructor (width, height) {
        this._width  = width
        this._height = height
    }
    set width  (width)  { this._width = width               }
    get width  ()       { return this._width                }
    set height (height) { this._height = height             }
    get height ()       { return this._height               }
    get area   ()       { return this._width * this._height }
}
var r = new Rectangle(50, 20)
r.area === 1000

Important to note that there is no requirement to damage the prototype in ES6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.