3

Let's say we have these 2 classes, What are the differences of having a function in the module scope vs being a static class function, assuming doSomething doesn't require the this access. the only difference I can see is, module is "more private", is there any thing else?

Sample1.js

class Sample1 extends React.Component {
    static doSomething(input) {
        // ...
        return input2;
    }

    render() {
        if (Sample1.doSomething(x)) {
            return <div />;
        }
        return null;
    }
}
export { Sample1 };

Sample2.js

const doSomething = (input) => {
    // ...
    return input2;
};

class Sample2 extends React.Component {
    render() {
        if (doSomething(x)) {
            return <div />;
        }
        return null;
    }
}
export { Sample2 };
3
  • Well, the differences are how you can call it and from where you can access it. Aren't those enough already? Commented Jan 11, 2018 at 0:25
  • not sure, is there like memory usage difference, for example as static it may need to reserve memory per each object instance. ? Commented Jan 11, 2018 at 0:27
  • 1
    No, static means exactly that it's not per-instance. Commented Jan 11, 2018 at 0:59

2 Answers 2

2

Well, in Sample1 doSomething is exposed, you can access it with Sample1.doSomething(), but in Sample2 scenario, the method won't be available outside the module scope, so yeah it is private.

Now, about the memory consumption, no matter how many instances you create, that static method it is allocated once, and it is accessible through the class only, not through the instances.

Does this make sense to you?

Sign up to request clarification or add additional context in comments.

6 Comments

FYI, even member functions would only be allocated despite how many instances are constructed anyway. Their reference is what's allocated for each instance, which is basically the size of a pointer.
Yep, exactly, they are prototype methods, they are assigned to the prototype of the created instance, not the instance itself. Like doing Sample1.prototype.myMethod = ...
@PatrickRoberts Not exactly. If the member function is inherited from the prototype, it doesn't even allocate an extra pointer in the instance. If a member function is created for every instance, it's much more than just one pointer. Having a reference (pointer) to a shared function on every instance would be possible but is really uncommon.
@Bergi in case my mistake wasn't clear, when I edited my comment, I accidentally removed the word "once" before "despite". I was also referring to the pointer to the prototype chain per instance (i.e. this.__proto__, for lack of a better example to use), not for the member function directly (i.e. this.doSomething() if it was a member method). I probably should have been clearer about that.
In case the method does not use any instance data and it's a class specific helper method which I don't need to "expose" it, would you recommend make it as static or just as a module function?
|
0

The only difference is testability. When a function is called as a method like Sample1.doSomething(x), it can be spied or stubbed during testing.

A way that is more extendable is to refer static method like this.constructor.doSomething(x). In this case the method can be extended in child class.

If the method is used only in class instances and never called as static (like in React component), it doesn't make sense to make it static. It can be defined as prototype method and be called like this.doSomething(x).

2 Comments

If the method does not use any instance data, it totally makes sense to use a static method not a prototype one. Regardless from where it is called.
Why? The situation can be turned upside down, and the statement 'if the method isn't supposed to be called as static, it shouldn't be static' will have same validity. To my knowledge, performance concerns for proto methods are insignificant, and having an opportunity to easily refer it through this really mattrs, exactly for the reasons explained above. In other aspects it's a matter of taste.

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.