0

I want to create a variable inside a javascript class without the necessity to call it by a variable with the constructor.

for example, we have a class A that takes in 2 inputs (a and b). a and b are summed and the result is appended to an array arr. Where´s the best place to declare the array?

class A {
    constructor(a,b) {
        this.a = a;
        this.b = b;};

do_something(this.a, this.b) {
         let c = this.a + this.b;
         arr.append(c);
         return arr;}
};

where should i initialize arr without having it to be called form the outside ?

  • 2
    What do you want the scope of `arr` to be? Shared between every instance, or tied to a single instance? What's the problem with declaring it in the constructor? – CertainPerformance May 19 '20 at 14:04
  • 1
    So you want the array to be tied to the instance, but not set in the constructor? What exactly is the problem you're trying to solve? It's not very clear – CertainPerformance May 19 '20 at 14:12
  • @CertainPerformance Let´s assume i want the output to be in the form of an array. I want the empty array to be initialized whenever i instanciate a class. –  May 19 '20 at 14:15
  • 1
    It *should* be initialised in the constructor, as that's where properties are created. What's wrong with that, what is the problem that makes you avoid the obvious solution? – Bergi May 19 '20 at 14:16
  • @MHot So if you did call `do_something()` twice, you would expect it to append to the same array multiple times? And return a reference to the same array from both calls? – Bergi May 19 '20 at 14:17
  • @Bergi yes, exactly. Is the constructor still the right place to declare this kind of variable? –  May 19 '20 at 14:21
  • @MHot Yes, it is. What makes you assume anything else? (Btw, it's called a property, not a variable) – Bergi May 19 '20 at 14:23
  • @Bergi, its causing problems, thats why. Declaring arr1 = []; inside a constructor doesnt work well for me. –  May 19 '20 at 14:29
  • @MHot It's `this.arr = [];`. Have a look at https://stackoverflow.com/q/13418669/1048572?javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object – Bergi May 19 '20 at 16:20

1 Answers1

0

At the beginning of the class definition, also you can not have this.a and this.b as parameters of the do_something.

class A {
arr = []
    constructor(a,b) {
        this.a = a
        this.b = b
}
do_something() {
         const c = this.a + this.b
         this.arr.push(c)
         return [this.arr, c]}
}

const obj = new A(1,2)

console.log(obj.do_something())
radulle
  • 1,437
  • 13
  • 19
  • thanks! could you explain the logic behind the fact that i cannot use this.a and this.b as paramters of the class method? –  May 19 '20 at 14:05
  • 2
    This is effectively the same thing as declaring the `arr` in the constructor. You also aren't referencing `arr` properly, and `append` is not a method of arrays – CertainPerformance May 19 '20 at 14:06
  • However, after trying this out i got this error: "SyntaxError: Unexpected token =". –  May 19 '20 at 14:09
  • @CertainPerformance duly noticed and fixed. Check now. I agree with you it is same as declaring it in the constructor. – radulle May 19 '20 at 14:09
  • @MHot try now, I forgot to add `this.` in a few places. – radulle May 19 '20 at 14:10
  • @CertainPerformance You are right, but would like to create an variable inside a class. Do you have an idea? (append is for sure not a js array method, but this doesnt matter right now) –  May 19 '20 at 14:10
  • @MHot no problem, please check the mark as answered. – radulle May 19 '20 at 14:13
  • @MHot ... if one accepts this answer then technical nothing has changed in comparison to declaring the array publicly inside the constructor function via `this.arr = [];`. As for the example code of @radulle one easily can log `obj.arr` which equals `[3]`. You might be interested in [private instance fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_instance_fields) via its *hash name syntax*. – Peter Seliger May 20 '20 at 16:12