-1

Well, I am kinda surprised that I had to ask a question about this but most examples provide a getter and setter but I have not seems a functions that takes parameters in es6 classes.

Given the following example from MDN web docs.

class Rectangle {


constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get area() {
    return this.calcArea();
  }

  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area);

How can I add a method that takes n amount of arguments and returns something.

Example in old js

var doSomething = function(theThing) {
    // process something 
    return theThingProcessed;
}
Beto
  • 806
  • 3
  • 12
  • 33
  • `calcArea` is defined as `calcArea() { … }` and called as `this.calcArea()`. If you wanted a function `foo` to take an argument and be called as `this.foo(arg)`, can you guess what the syntax would be? – Ry- Sep 17 '17 at 03:44
  • It's identical to regular function declarations for declaring and using arguments. – jfriend00 Sep 17 '17 at 03:47
  • Read the question again. @Ryan – Beto Sep 17 '17 at 03:48
  • @Beto: Why, did you change something? – Ry- Sep 17 '17 at 03:48

1 Answers1

0

Like you did in the constructor except with a name?

class Rectangle {


constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get area() {
    return this.calcArea();
  }

  calcArea() {
    return this.height * this.width;
  }

  doSomething(theThing,theThing2,...n){
    //process something


    return theThingProcessed;
  }
}

const square = new Rectangle(10, 10);
square.doSomething(theThing);
console.log(square.area);
Eladian
  • 958
  • 10
  • 29