3

If I have this:

class Math {
  static add(a, b) {
    return a + b
  }
}

and I want turn it into:

class Math {
  static add(a, b) {
    return a + b
  }
  static subtract(a, b) {
    return a - b
  }
}

Is there a way to do it dynamically? eg.

class Math {
  static add(a, b) {
    return a+b
  }
}

Math.extend({
  subtract: function(a, b) {
     return a-b
  }
})

Math.subtract(1,1) // 0

5 Answers 5

10

Static methods are nothing but methods on the constructor. So all you need to do is assign the methods to Math:

Object.assign(Math, {
  subtract(a, b) {
    return a - b
  }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Why did you use Object.assign() when Math.subtract = function() {...} should work just fine?
Because OP used Math.extend(...) making it look like they might want to add more methods, in which case Object.assign might be more useful. But whatever works is fine of course :)
its a common pattern to use assign for extending prototype as this is the only way to extend it with property accessors.
accepting this answer cause of the detail about how I preferred an "extend" similar syntax :)
@kidcapital - The method in other answers (including my answer) of directly assigning a property to the Math object as in Math.subtract = function(a, b) {...} works just fine even in ES3, ES4, ES5. Or you could use Object.defineProperty() too in ES5+. Object.assign() just allows you to attach multiple static methods in one function call, but you can just direct assign each method individually without using Object.assign() and have wide browser compatibility.
|
3

Static methods are just methods on the constructor, so you can just define one at any time like this:

Math.subtract = function(a, b) {
    return a - b;
}

Note: This will work in ANY browser currently in use as it does not rely on any new or recent features.

1 Comment

Why the downvote? What's wrong with this method of creating a static method?
1

When compiling this down to ES5 you essentially get this:

function Math() {
}
Math.add = function(a, b) { return a + b; }

So following that, you can add a dynamic static method like this:

Math.subtract = function(a, b) {
  return a - b;
}

Or since you're using ES6, you could use an arrow function:

Math.subtract = (a, b) => a - b;

6 Comments

you want the static method's this to point to the class - so the arrow function is not the best idea
@PeterAronZentai: Good point, I don't think people usually use this in static methods. Too confusing with this in instance methods.
thats the only way to see what class was it invoked on - it also inherits down the chain.
@PeterAronZentai Maybe so but this function doesn't even use any member properties so it's safe to use an arrow function.
@PeterAronZentai: If it's about spec compliance, yo';d also have to use Object.defineProperty since methods are not enumerable. Also not sure about super...
|
0

The simplest way is to assign the property to the class:

class Test {}

Test.method = function() {
    alert("works");
};

Test.method(); // works

https://jsfiddle.net/byppz1vt/1/

Comments

-2

You can add static methods via Math.subtract = function() { // code here };.

1 Comment

This does not create a static method. This creates an instance method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.