7

I have a class that looks like this:

class Foo {
    constructor(arg1, arg2) {
        // ...

        this._some_obj = new SomeObj({
            param1: arg1,
            param2: arg2
        });
    }

    // ...
}

module.exports = Foo;

Now I want to do the same thing but with _some_obj shared between all instances of the class.

After searching around I'm unclear as to the correct way to do this in ES6.

3
  • well.. how would you do it in es5? Commented Apr 28, 2016 at 18:02
  • If it's shared between all instances, what is the expected behavior for the arguments? Would the first call initialize it with whatever the first args happen to be? Commented Apr 28, 2016 at 19:06
  • @loganfsmyth - Yes I hadn't really thought this through in that regard. I assume there should be some type of initialization function instead of putting those args in the constructor. Commented Apr 28, 2016 at 19:21

2 Answers 2

7

As known from ES5, you can just put it on the class's prototype object:

export class Foo {
    constructor(arg1, arg2) {
        …
    }
    …
}
Foo.prototype._some_obj = new SomeObj({
    param1: val1,
    param2: val2
});

Or directly on Foo, if you don't need to access it as a property on instances.

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

6 Comments

I read that defining properties on the prototype is a bad idea and that this is why it wasn't included in ES6 classes in the first place. Is that not true?
@SeanLynch it's not a good diea to REdefine prototype properties on classes you didn't make. For instance, Math.prototype.min = is not a good property to overwrite, because it effects every instance of Math.min
@SterlingArcher - I see. I didn't really understand that. Thank you!
adding to classes you own isn't the same as adding to native objects prototypes (which can seriously mess with any libraries you may use)
In this case, how would I get the values of arg1 and arg2 to be available to the constructor of SomeObj. I think Foo.prototype._some_obj would be created before the constructor is ever called.
|
3

Use static to have class properties.

class MyClass {
  static myStaticProp = 42;

  constructor() {
    console.log(MyClass.myStaticProp); // Prints '42'
  }
}

N.B: this is a feature already implemented in Babel, but is still experimental as only at the 1st proposal stage.

5 Comments

As I understand it, static is only for member functions not data.
But in my case I'd have to initialize myStaticProp with the arguments to the constructor.
@SeanLynch you can initialize to null and, in the constructor, have an if (this.myStaticProp === null) { ... }
@SeanLynch your understanding is incorrect, otherwise Object.defineProperties(MyClass, ...) would behave much differently.
OP asked for ES6 solutions

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.