如何用class.js声明一个静态变量?

时间:2015-10-23 20:43:29

标签: 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;
}

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

1 个答案:

答案 0 :(得分:1)

如果通过"静态"你的意思是全班,你只需将它作为一个属性分配给你的"类" (构造函数),例如:

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

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

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

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

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

...然后它是A)真正私密的你的"班级,"和B)没有类前缀。

如果没有提到Resig的SJI非常基本且过时,那么这个主题的答案就不会完整。这些天,我建议使用ES2015(ES6)语义和一个体面的转换器(我使用Babel)。我坦率地对SJI使用函数反编译处理超级调用感到高兴,特别是因为它不是很严格,很容易被“超级"”这个词搞糊涂。出现在字符串文字中,所以我wrote my own thing instead(现在也已经过时了ES2015)。但是,有趣的是,ES2015类语义只提供静态方法,而不是静态变量,所以你最终会做与上面相同的事情:

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

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

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

    return Thingy;
})();