Where to declare class constants?

后端 未结 9 1407
悲&欢浪女
悲&欢浪女 2021-02-18 14:26

I\'m using class members to hold constants. E.g.:

function Foo() {
}

Foo.CONSTANT1 = 1;
Foo.CONSTANT2 = 2;

This works fine, except that it see

9条回答
  •  无人及你
    2021-02-18 14:37

    IF the constants are to be used inside of the object only:

    function Foo() {
        var CONSTANT1 = 1,CONSTANT2 = 2;
    }
    

    If not, do it like this:

    function Foo(){
        this.CONSTANT1=1;
        this.CONSTANT2=2;
    }
    

    It's much more readable and easier to work out what the function does.

提交回复
热议问题