最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

object oriented javascript how to create a static class for constants - Stack Overflow

programmeradmin5浏览0评论

I've decided to put all my constants in a neat class called constants, and would like to access its members with the dot operator.

So far I've tried:

function Constants(){
    Constants.style = {};
    Constants.style.border_sides = 20;
    Constants.style.button_width = 17;
    // ...
}

And later

Constants = new Constants();

and

{
    $('#button').width(Constants.style.button_width);
}

That resulted in a

Can't access button_width of undefined.

I would use JSON to declare the constants, but I like ments in my code. Would someone explain javascript's OO?

I've decided to put all my constants in a neat class called constants, and would like to access its members with the dot operator.

So far I've tried:

function Constants(){
    Constants.style = {};
    Constants.style.border_sides = 20;
    Constants.style.button_width = 17;
    // ...
}

And later

Constants = new Constants();

and

{
    $('#button').width(Constants.style.button_width);
}

That resulted in a

Can't access button_width of undefined.

I would use JSON to declare the constants, but I like ments in my code. Would someone explain javascript's OO?

Share Improve this question asked Nov 11, 2011 at 16:59 lowerkeylowerkey 8,33518 gold badges70 silver badges102 bronze badges 1
  • The object literal suits my purposes pretty well. – lowerkey Commented Nov 11, 2011 at 17:53
Add a ment  | 

4 Answers 4

Reset to default 5

You replace the Constants function with an instance of Constants. And you applied your constants to the function, not an instance of Constants or the prototype. So you effectively wiped your constants away.

I'd argue just use an object literal

var constants = {
   style: {
        border_sides: 20
   }
};

Keep in mind there's nothing actually constant about either approaches. Anyone can easily change the "constant" values. If you want truly constant data, you might want to use getters/setters, Object.defineProperty or the module pattern.

If you want a "class" with both static and instance methods:

function ClassName(){

}

ClassName.prototype = { //Put instance methods here
   instanceMethod1 : function(){},

   instanceMethod2 : function(){}
};

ClassName.staticMethod1 = function(){};
ClassName.staticMethod2 = function(){};

var a = new ClassName();

a.staticMethod1; //undefined
a.instanceMethod1; //function(){};

ClassName.staticMethod1 //function(){};

Why don't you just consider using literals?

var Constants = {
    style: {
        border_sides: 20,
        button_width: 17
    }
}

Even if you are willing to consider using the construction function, replace Constants with this

function Constants(){
    this.style = {};
    this.style.border_sides = 20;
    this.style.button_width = 17;
    // ...
}

You don't need to do any new Constants() for literals (1st example). Just start using right away.

For 2nd example (constructor function) you need to do var constants = new Constants().

In Javascript everyting is an object, so you're adding properties to the Constants function within the function.

To get the effect you want write:

var Constants = {
  someText: 'text1',
  someInt: 1
};

And to access, is simply:

var text = Constants.someText;
发布评论

评论列表(0)

  1. 暂无评论