Home > front end >  Accessing arguments from a class passed down from the constructor into a method using rest
Accessing arguments from a class passed down from the constructor into a method using rest

Time:12-15

I am trying to pass each argument through the constructor into the method which in turn encrypt the values. But, the Method isn't reading the values in the arguments.

Please, what am I doing wrong?

class Add {
    constructor(...words) {
        this.words = words;

    }

print(words){
    words = [words];
    let output = "$"
console.log(words)
    for(let word of words){
        output  = word  "$"
    }
return output
}

    
}

var x = new Add("I", "Love","you");
var y = new Add("this", "is", "awesome");

x.print()
z.print()

CodePudding user response:

You misspelled constructor and also if you assign something to this inside of constructor then you can also access that on this in other methods.

class Add {
  constructor(...words) {
    this.words = words;
  }

  print(words) {
    let output = "$"
    for (let word of this.words) {
      output  = word   "$"
    }
    return output
  }
}

var x = new Add("I", "Love", "you");
console.log(x.print())

  • Related