javascript - 如何用class.js声明静态变量?

标签 javascript class oop

我使用 John Resig 的 class.js:

/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

// The base Class implementation (does nothing)
Class = function() {};

// Create a new Class that inherits from this class
Class.extend = function(prop) {
    var _super = this.prototype;

    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;

    // Copy the properties over onto the new prototype
    for (var name in prop) {
        // Check if we're overwriting an existing function
        prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
            (function(name, fn){
                return function() {
                    var tmp = this._super;

                    // Add a new ._super() method that is the same method
                    // but on the super-class
                    this._super = _super[name];

                    // The method only need to be bound temporarily, so we
                    // remove it when we're done executing
                    var ret = fn.apply(this, arguments);
                    this._super = tmp;

                    return ret;
                };
            })(name, prop[name]) :
            prop[name];
    }

    // The dummy class constructor
    Class = function () {
        // All construction is actually done in the init method
        if ( !initializing && this.init )
            this.init.apply(this, arguments);
    }

    // Populate our constructed prototype object
    Class.prototype = prototype;

    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;

    return Class;
};

if(!(typeof exports === 'undefined')) {
    exports.Class = Class;
}

如何使用它在新类中声明静态变量?

最佳答案

如果“静态”是指类范围内的,则只需将其分配为“类”(构造函数)上的属性,例如:

var Thingy = Class.extend({
    init: function() {
        // ...
    }
});
Thingy.myStaticVariable = "foo";

使用时必须使用完全限定名称。

当然,您可以通过创建关闭的上下文来创建“静态”变量:

var Thingy = (function() {
    var myStaticVariable = "foo";

    return Class.extend({
        init: function() {
            // ...
        }
    });
});

...然后它是 A) 真正私有(private)于您的“类”,并且 B) 无需类前缀即可使用。

<小时/>

如果不提及 Resig 的 SJI 非常基础且相当过时,这个主题的答案就不会真正完整。如今,我建议使用 ES2015 (ES6) 语义和一个不错的转译器(我使用 Babel )。坦率地说,我对 SJI 使用函数反编译来处理 super 调用一直不满意,特别是因为它不严格,并且很容易被字符串文字中出现的“ super ”一词混淆,所以我 wrote my own thing instead (现在 ES2015 也已过时)。但有趣的是,ES2015 类语义仅提供静态方法,而不是静态变量,因此您最终会执行与上述相同的操作:

class Thingy {
    constructor() {
        // ...
    }
});
Thingy.myStaticVariable = "foo";

let Thingy = (function() {
    let myStaticVariable = "foo";

    class Thingy {
        constructor() {
            // ...
        }
    }

    return Thingy;
})();

关于javascript - 如何用class.js声明静态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33311091/

相关文章:

javascript - 如何在javascript中调整原始数据的大小?

php - 如果对象存在,如何重用它

perl - 如何在Perl中明确销毁对象?

c++ - 为什么我的类(class)规模为零?如何确保不同的对象具有不同的地址?

php - XXX 不是有效的实体或映射的父类(super class)//和配置选项

c++ - 在类堆栈之间交换数据

Javascript:迭代数组会导致无限循环?

javascript - 仅显示段落的前两行

javascript - 未捕获的语法错误 : missing ) after argument list 5

javascript - EmberJS : Injecting owner to native class from component