开发者

Anonymous class with MooTools

Can I create the an开发者_StackOverflow中文版onymous class(java term) using the MooTools js framework? Suppose it should look like:

A = new Class({
  invoke: function() {
    alert('class');
  } 
});

a = new A({
  invoke: function() {
    this.parent();
    alert('instance');
  },
});

But I don't want to modify constructor interface around to accept additional properties. Is there more comfortable way to define classes that are used not more than once?


You could define the class only within the scope it is going to be used in, which means the class won't be available outside that scope. For example, using a self-executing function:

(function() {

    var MyClass = new Class({
      invoke: function() {
        alert('class');
      } 
    });

    var myObject = new MyClass({
      invoke: function() {
        this.parent();
        alert('instance');
      }
    });

})();

(I've fixed up several errors in your code, including omitting the var keyword when declaring MyClass and myObject, and an extra , when initialising myObject.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜