zoukankan      html  css  js  c++  java
  • JavaScript基础对象创建模式之对象的常量(028)

    虽然许多编程语言提供了const关键字来支持常量的声明,但JavaScript里没有表示常量的语义。我们可以用全大写的方式来声明变量,表明它实际上是个常量:

    Math.PI; // 3.141592653589793
    Math.SQRT2; // 1.4142135623730951
    Number.MAX_VALUE; // 1.7976931348623157e+308
    
    通常这种常量会用对象来包装起来:比如上面的Math。要实现程序里需要的常量,可以这样做:
    // constructor
    var Widget = function () {
        // implementation...
    };
    // constants
    Widget.MAX_HEIGHT = 320;
    Widget.MAX_WIDTH = 480;
    
    同样的思路,可以使通过字面声明方式创建的对象也可以有全大写的属性。但上面的实现并不能阻止用户改变“常量”(实际上是变量)的值。为了解决这一问题,可以声明一个专门保存常量的对象,比如叫consot,并通过下面的3个方法来统一提供常量的定义与值的获取:
    • set(name, value) 定义一个名为name的常量,值为value;
    • isDefined(name) 检查一个名为name的常量有没有被定义;
    • get(name) 得到名为name的常量的值
    在下面的const实现中,只支持简单类型的常量数据。通过hasOwnProperty() 来检查数据类型是否可用,以及常量是否已经被定义:
    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;
            }
        };
    }());
    
    检查这个实现:
    // 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"); // 480
    
  • 相关阅读:
    千峰公开课徐老师讲解前端前沿技术总结
    网站被k到可以使用关键词搜索到首页优化总结
    SEO基础内容
    react项目后台及上线步骤
    超好用的图片压缩工具,推荐给你
    属性title样式自定义
    Vue.js_devtools_5.1.0.zip【需要的可自行下载】
    js异步执行 按需加载 三种方式
    Web安全之跨站脚本攻击(XSS)
    操作数组不要只会for循环
  • 原文地址:https://www.cnblogs.com/Bryran/p/3976151.html
Copyright © 2011-2022 走看看