IamCarbonMan IamCarbonMan - 24 days ago 4
Javascript Question

Why does Math.min.apply not work within a class?

The following code works in Node v7.0.0:

Math.min.apply(null,[1,2,3]) // returns 1


But this doesn't:

class foo {
min(args) {
Math.min.apply(null,args);
}
}
bar = new foo();
bar.min([3,4,5]) // returns undefined


Why is this?

Answer

You're missing a return statement:

class foo {
  min(args) {
    return Math.min.apply(null,args);
  }
}
Comments