0

I need to make my custom error. If the arguments are not numbers and if the arguments alength are more or less than two, then an error occurs.

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.isNum();
    }
    isNum() {
        if(typeof this.x !== 'number' || typeof this.y !== 'number' ) {
            throw new Error('point is not a number ')
        }
      console.log(arguments.length)
    } 
} 

try{
    let example = new Point(1,2)
} catch(e){ 
    console.error(e)
}
console.log(example.arguments.length)

How do I know the length of the arguments?

Is this spelling wrong?

If you can show how to solve correctly?

I apologize if I crookedly wrote a question.

incognita
  • 45
  • 5

1 Answers1

1

Using arguments object

Note: example is defined inside try with let which cant be accessed from outside of scope

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.arguments = arguments;
    this.isNum();
  }
  isNum() {
    if (this.arguments.length != 2) {
      throw new Error('you send more arguments')
    }
    if (typeof this.x !== 'number' || typeof this.y !== 'number') {
      throw new Error('point is not a number ')
    }
  }
}

try {
  let example = new Point(1, 2)
  console.log(example.arguments.length)
} catch (e) {
  console.error(e)
}
User863
  • 19,346
  • 2
  • 17
  • 41