Where to declare class constants?

后端 未结 9 1434
悲&欢浪女
悲&欢浪女 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:56

    If you're using jQuery, you can use $.extend function to categorize everything.

    var MyClass = $.extend(function() {
            $.extend(this, {
                parameter: 'param',
                func: function() {
                    console.log(this.parameter);
                }
            });
            // some code to do at construction time
        }, {
            CONST: 'const'
        }
    );
    var a = new MyClass();
    var b = new MyClass();
    b.parameter = MyClass.CONST;
    a.func();       // console: param
    b.func();       // console: const
    

提交回复
热议问题