【问题标题】:Best practices for static methods and variables with MooTools classesMooTools 类的静态方法和变量的最佳实践
【发布时间】:2011-05-17 09:00:56
【问题描述】:

是否有任何最佳实践或通用解决方案可以向 MooTools 生成的类添加对“静态”方法和变量的支持?

具体来说,是否有任何解决方案可以确保在调用实例initialize 方法之前进行静态初始化?

【问题讨论】:

    标签: javascript oop static mootools


    【解决方案1】:

    警告:从未使用过 MooTools。不过,我用过 Prototype,它有一个类似的 Class 系统(MooTools 要么是“受启发”,要么是 Prototype 的一个分支,这取决于你问谁)。

    只需将它们作为属性添加到生成的“类”上:

    var MyClass = new Class(properties);
    MyClass.staticMethod = function() {
        // ...
    };
    

    (上面第一行来自the docs;其余是我的补充。)

    您知道这将在 initialize 之前在任何新实例上发生,因为您不会在附加静态方法(或属性)之前留下创建新实例的机会。

    【讨论】:

    • staticMethod 将不可用于 MyClass 的实例,不确定这是否需要,但请记住这一点。您可以致电MyClass.staticMethod() - 为了提高可读性,请使用MyClass.extend({ method: function() ... , method2: function() ...});
    • @Dimitar: staticMethod 将可用于MyClass 的实例,就像其他任何东西一样,如MyClass.staticMethod()
    • 就是这样,它不可用(根据我的经验)-jsfiddle.net/fWQBM->您可以在 MyClass 上调用它,但不能在实例上调用它。 .extend 相同 - 但您可以直接向实例添加方法。 mootools !=== 原型
    • @Dimitar:对。 OP 询问如何添加 static 方法。这是这样做的。如果你想要实例方法,你可以在你交给new Class的属性中定义它们。
    • 我知道,我只是想我应该提一下,因为当我第一次尝试向其他地方定义的类添加新方法时,它让我很反感 :)
    【解决方案2】:

    我知道这篇文章已经过时了,但我想给出一个比已经说明的更好的答案。
    我建议静态方法使用以下语法:

    var MyClass = new Class({
        initialize: function() {
            this.method();
            MyClass.staticMethod();
        }
        ,
        method: function() {}
    }).extend({
        staticMethod: function() {}
    });
    

    .extend({}) 方法是在类上添加静态方法的标准方法。

    我唯一不喜欢的是MyClass.staticMethod(); 语法,但没有太多更好的选择。

    【讨论】:

      【解决方案3】:

      开胃菜...

      JavaScript 中的静态方法是引用它们的对象的属性。它们不会添加到 Object 的原型中。

      有两种方法可以在 JavaScript 中向对象添加函数。下面,我向一个名为“MyObject”的虚构对象添加方法。

      1. 房产

        MyObject.staticMethod = new function() {};
        
        MyObject.staticMethod(); // Call static method.
        
      2. 方法

        MyObject.prototype.instanceMethod = new function() {};
        
        new MyObject().instanceMethod(); // Call instance method.
        

      主菜...

      向类添加静态方法的方法有三 (3) 种。以下代码源自 Mark Obcena 的 “Pro JavaScript with MooTools”

      我提供了一些 Arcabard 的回答中缺少的更多信息。

      1。作为对象属性

      var Person = new Class({
          // Instance Variables
          name: '',
          age: 0,
          // Constructor
          initialize: function(name, age) {
              this.name = name;
              this.age = age;
          },
          // Instance Methods
          log: function() {
              console.log(this.name + ', ' + this.age);
          }
      });
      
      // Static Property
      Person.count: 0;
      // Static Methods
      Person.addPerson: function() {
          this.count += 1;
      };
      Person.getCount: function() {
          console.log('Person count : ' + this.count);
      };
      

      2。使用extend()

      var Person = new Class({
          // Instance Variables
          name: '',
          age: 0,
          // Constructor
          initialize: function(name, age) {
              this.name = name;
              this.age = age;
          },
          // Instance Methods
          log: function() {
              console.log(this.name + ', ' + this.age);
          }
      });
      
      Person.extend({
          // Static Property
          count: 0,
          // Static Methods
          addPerson: function() {
              this.count += 1;
          },
          getCount: function() {
              console.log('Person count : ' + this.count);
          }
      });
      

      3。向Class.Mutators 添加一个新的mutator

      // This will create a shortcut for `extend()`.
      Class.Mutators.Static = function(members) {
          this.extend(members);
      };
      
      var Person = new Class({
          Static: {
              // Static Property
              count: 0,
              // Static Method
              addPerson: function() {
                  this.count += 1;
              },
              getCount: function() {
                  console.log('Person count : ' + this.count);
              }
          },
          // Instance Variables
          name: '',
          age: 0,
          // Constructor
          initialize: function(name, age) {
              this.name = name;
              this.age = age;
          },
          // Instance Methods
          log: function() {
              console.log(this.name + ', ' + this.age);
          }
      });
      

      使用静态方法的示例。

      // Creating a new Person instance
      var mark = new Person('Mark', 23);
      mark.log();
      
      // Accessing the static method
      Person.addPerson();
      Person.getCount() // 'Person count: 1'
      

      【讨论】:

        【解决方案4】:

        摘自“Pro Javascript with Mootools”

        extend 方法通过Function.prototype 声明并被所有函数继承。因为 MooTools 类本质上是一个函数,所以我们也可以对类使用 extend 方法。该方法与 implementation 方法相似,因为它接受一个对象参数,其中键和值引用将添加的成员。但是,与 implement 不同的是,extend 不会将成员添加到类的原型中,而是添加到类对象本身。

        // You can implement a `Static` mutator for creating static methods and variables:
        Class.Mutators.Static = function(members) {
            this.extend(members);
        }
        
        // Using the Static mutator above
        var Person = new Class({
            Static: {
                // Static Property
                count: 0,
                // Static Method
                addPerson: function() {
                    this.count += 1;
                },
                getCount: function(){
                    console.log('Person count: ' + this.count);
                }
            }
        });
        

        更多信息请参见MooTools: Types > Function > extend()

        【讨论】:

        • -1 您在这里使用的“Static:” mutator 不是 MooTools 的一部分——它实际上是 sample code from earlier in the book。另外,这个例子甚至没有展示如何使用.extend()
        猜你喜欢
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 2015-10-03
        • 2017-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多