views:

36

answers:

1

Can I create the anonymous 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?

+1  A: 

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.)

Steve Harrison