6

我指的是这篇文章

在其中,他定义了一个看起来像这样的函数:

function makeClass() {
    return function _class() {
        if(this instanceof _class) {
            if(typeof this.init === 'function') {
                this.init.apply(this, arguments);
            }
        } else {
            throw new Error('Constructor called as a function');
        }
    };
}

然后你可以用它来做这样的事情:

var MyClass = makeClass();

MyClass.prototype = {
    init: function(width, height) { ... },
    clear: function(ctx) {... },
    draw: function(ctx) { ... }
}

但现在我想初始化一些应该在所有实例之间共享的静态变量。我怎么做?

4

6 回答 6

4

好吧,最简单的方法是将静态变量定义为原型属性:

MyClass.prototype.xxx: 3, // ...
var t1 = new MyClass();
console.log(t1.xxx); // 3

...但它不会像其他语言中的静态属性通常那样:

var t2 = new MyClass();
t2.xxx = 5;
console.log(t1.xxx); // still 3 :(

另一种方法是利用属性也可能附加到函数的事实:

MyClass.xxx = 3;

...但这会缩小我们使用此属性的方式(不能t1.xxx从前面的示例中调用它)。

不过,还有另一种方式。可以将静态属性定义为变量、方法的局部变量、init方法可访问的、定义的……在这个 init 方法中也是如此。) 像这样。

   init: function() {
      var xxx = 3;
      MyClass.prototype.getXXX = function() {
         return xxx;
      };
      MyClass.prototype.setXXX = function(newXXX) {
         xxx = newXXX;
      }
   }

然后所有人都可以通过以下方式使用此属性:

  var t1 = new MyClass();
  var t2 = new MyClass();
  console.log(t1.getXXX()); // 3
  console.log(t2.getXXX()); // 3
  t1.setXXX(5);
  console.log(t1.getXXX()); // 5 now
  console.log(t2.getXXX()); // 5 as well, behold the power of closures!

这是一个使用的小提琴

更新:我想,当我们需要使用静态类数据的(某种)容器时,最好使用这种方法,该容器将由所有对象共享 - 但我们并不确切知道实际上可以是什么存放在这个容器中。然后我们只使用两个函数 -getStaticsetStatic- 通过字符串键或其他标识符来存储和检索数据。这似乎有点令人困惑,确实如此,但我认为这可能值得努力。)

于 2012-06-23T21:06:36.137 回答
3

只需将其添加到MyClass自身。

MyClass.myVariable = 42;

在 Java/C# 意义上,它并不是真正的静态,但会给您相同的效果。

于 2012-06-23T20:59:30.943 回答
2

我通过使用命名约定“解决”了这个问题。

我想要Class.extend({ })语法的便利,但也想要一种在其中声明“静态”属性的方法。

我选择了一个前导下划线来声明一个静态属性,尽管你可以做任何你喜欢的事情。

用法:

var myClass = Class.extend({

    _staticProperty: 1337

    , instanceProperty: 'foo'
    , instanceMethod: function() { }

    , ctor: function() {
        this.base();
    }
});

注意我已经重命名initthis._super()来自原始代码

和代码:

/* Simple JavaScript Inheritance
 * Modified by Andrew Bullock http://blog.muonlab.com to add static properties
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function () {
    var initializing = false, fnTest = /xyz/.test(function () { xyz; }) ? /\bbase\b/ : /.*/;

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

    // Create a new Class that inherits from this class
    Class.extend = function (prop) {
        var base = 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;

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

        // Copy static properties from base
        for (var name in this) {
            if (name.substr(0, 1) == '_')
                Class[name] = this[name];
        }

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

                        // Add a new .base() method that is the same method
                        // but on the super-class
                        this.base = base[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.base = tmp;

                        return ret;
                    };
                })(name, prop[name]);
            } else if (name.substr(0, 1) == '_') {
                Class[name] = prop[name];
            } else {
                prototype[name] = prop[name];
            }
        }

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

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

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

        return Class;
    };
})();
于 2014-05-04T16:48:57.830 回答
1

如果您不关心浏览器支持,您还可以使用WeakMap构造函数/静态属性对。这是想法:http: //jsfiddle.net/DfNNU/2/。这需要MyClass.prototype.constructor,您不应该丢弃它。因此,您需要添加回constructor: MyClass原型。

var statics = (function() {
    var map = new WeakMap;
    return function(inst) {
        var ctor = inst.constructor;
        return map.get(ctor) || map.set(ctor, {});
    };
})();

像这样使用它:

var a = function() {};
var b = function() {};

var inst1 = new a;
var inst2 = new a;
var inst3 = new b;

statics(inst1).foo = 123;
statics(inst3).foo = 456;

console.log( statics(inst1).foo );  // 123
console.log( statics(inst2).foo );  // 123
console.log( statics(inst3).foo );  // 456
于 2012-06-23T21:16:30.570 回答
1

我已经修改了 John Resig 的类,将父类的静态成员复制到新类中,它添加了以下内容:

    for (var name in this) {
        if (!Class[name]) {
            Class[name] = this[name];
        }
    }

这是一个小提琴

// This is a modified version of John Resig's simple inheritence class to add copying of static methods
// The new code is the for loop commented with "add in the static members"

/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
 
  // The base Class implementation (does nothing)
  this.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
    function Class() {
      // 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.prototype.constructor = Class;

    //add in the static members
    for (var name in this) {
    		if (!Class[name]) {
		        Class[name] = this[name];
        }
    }

    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

function addText(text) {
    document.getElementById('greetings').innerHTML = document.getElementById("greetings").innerHTML + '<br>' + text;
}

//parent class with a prototype method and two static methods
var Parent = Class.extend({
  hello: function () {
    addText('parent.hello');
  }
});  
Parent.static = function() {
  addText('Parent.static');
}
Parent.overrideStatic = function() {
  addText('Parent.overrideStatic');
}

//child class that overrides one of the parent's static methods
var Child = Parent.extend();
Child.overrideStatic = function() {
  addText('Child.overrideStatic');
}

var parent = new Parent();
parent.hello();
Parent.static();

var child = new Child();
child.hello();   //should output parent.hello
Child.static();  //should output Parent.static
Child.overrideStatic();
<div id="greetings"></div>

于 2016-03-29T00:11:43.127 回答
0

在对“扩展”的调用中传入一个可选的静态成员列表。此方法将静态属性(如果有)添加到构造函数的“静态”属性。

代码更改

变化如下。这些行在“虚拟类构造函数”代码之后添加:

    if(staticProp) {
        Class.statics = [];
        for (var name in staticProp) {
            !Class.statics[name] && (Class.statics[name] = staticProp[name]);
        }
    }

声明类型时添加了一个附加参数“staticProp”,以便在此阶段引入静态成员:

Class.extend = function(prop,staticProp) {

可以在这里找到小提琴,包括一些测试。

使用示例

可以在类型声明时定义静态,就像使用第二个可选的构造函数参数一样:

var A = Class.extend({},{myStatic:1});

可以在实例方法中访问/定义静态:

var B = Class.extend({test:function(){B.statics.myStatic=2;}});

或从实例外部:

A.statics.myStatic=3;

使用 requirejs 的示例:

将 Class.js 放在 baseUrl 文件夹中。示例新类定义。不必将新类的文件命名为与“var C”(即 C.js)相同的名称,但可能更好地提高可读性,因此类方法中对 C 名称的引用与对其静态成员的任何外部引用保持一致:

define(['Class'],function($) {
    var C = Class.extend({
        init: function(params){
            C.statics.myStatic++; // access static data
        }
    },{
        myStatic: 123
    });
    return C;
});

D.js 中的另一个类是指 C 类中的静态数据:

define(['Class', 'C'],function($,C) {
    var D = Class.extend({
        init: function(params){
            C.statics.myStatic++; // static data of another class
        }
    },{});
    return D;
});
于 2017-08-01T23:51:07.283 回答