Principle

  1. Make variables shouldn't be changed stand out using all caps.
  2. Add constants as static properties to the constructor function.
// constructor

var Widget = function () {

    // implementation...

};

// constants

Widget.MAX_HEIGHT = 320;

Widget.MAX_WIDTH = 480;
  1. General-purpose constant object
set(name, value) // To define a new constant

isDefined(name)
 // To check whether a constant exists

get(name)
 // To get the value of a constant
var constant = (function () {

    var constants = {},

        ownProp = Object.prototype.hasOwnProperty,

        allowed = {

            string: 1,

            number: 1,

            boolean: 1

        },

        prefix = (Math.random() + "_").slice(2);

    return {

        set: function (name, value) {

            if (this.isDefined(name)) {

                return false;

            }

            if (!ownProp.call(allowed, typeof value)) {

                return false;

            }

            constants[prefix + name] = value;

            return true;

        },

        isDefined: function (name) {

            return ownProp.call(constants, prefix + name);

        },

        get: function (name) {

            if (this.isDefined(name)) {

                return constants[prefix + name];

            }

            return null;

        }

    };

}());

Testing the implementation:

// check if defined

constant.isDefined("maxwidth"); // false

// define

constant.set("maxwidth", 480); // true

// check again

constant.isDefined("maxwidth"); // true

// attempt to redefine

constant.set("maxwidth", 320); // false

// is the value still intact?

constant.get("maxwidth"); //

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.7 Object Constants的更多相关文章

  1. JavaScript Patterns 3.1 Object Literal

    Basic concept Values can be properties: primitives or other objects methods: functions User-defined ...

  2. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  3. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  4. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  5. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  6. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  7. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  8. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. .NET 中获取调用方法名

    在写记录日志功能时,需要记录日志调用方所在的模块名.命名空间名.类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下: 需要添加相应的命名空间: us ...

  2. Droid@screen:在PC屏幕上显示Android手机屏幕

    这里介绍一款工具——Droid@screen,用来获取手机屏幕,显示在PC屏幕上.它集截图.录像等多种功能于一体. 安装 1.    下载地址:http://droid-at-screen.org/d ...

  3. 管理系统-------------SSH框架书写登录和显示用户

    一.思路的穿插. web.xml中的配置找到--->application.xml---->找到对应的Action---->找到struts.xml----->在去找actio ...

  4. 《深入.NET平台和C#编程》内部测试题

    一 选择题 1)      以下关于序列化和反序列化的描述错误的是( C). a)      序列化是将对象的状态存储到特定存储介质中的过程 b)      二进制格式化器的Serialize()和D ...

  5. 第二章 部署war包到tomcat

    以turbine为例. 一.部署 1.下载或者生成war包(从maven上下载war包,并改名字为turbine.war) 2.将turbine.war拷贝到$TOMCAT_HOME/webapps中 ...

  6. 发测试 HTML/FILE/MYSQL/动态 20151120

    NilCMS几种页面输出方式: 1.直接生成html.不进行php处理. 2.生成文件缓存.针对于URL中单个目录文件过多,不利于管理.只进行PHP处理,不连接mysql. 3.生成mysql缓存.数 ...

  7. 用这个插件jquery.raty.js————用于星星评价(这个案例用于后台传数据给我,前台显示星星个数)

    1.引入jquery.js文件 2.引入jquery.raty.js 3.页面中放在这个:<span class="readOnly" data-path="ima ...

  8. 优秀的CSS框架---bootstrap

    Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它 由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架.现在在网上已经有 ...

  9. User Profile Service Application 配置同步连接时,报 MOSS MA not found

    最近在试着配置"我的网站",不知什么原因在配置同步连接时报:MOSS MA not found 搜索发现,需要启动Forefront Identity Manager Servic ...

  10. SharePoint Online 创建门户网站系列之创建栏目

    前 言 SharePoint Online的栏目,简单描述即显示在首页上的各个模块信息,这里,我们主要介绍我们首页上的栏目,包括简介类型.新闻列表类型.图片类型: 下面,让我们开始在SharePoin ...