How to add methods to all instances of JS.Class?

25 views
Skip to first unread message

Roman Snitko

unread,
Mar 5, 2013, 7:17:14 PM3/5/13
to jsclas...@googlegroups.com
So, I have a rather useful method I want to expose to all objects of classes that inherit from JS.Class. In other words, I need to extend JS.Class with useful functionality.
How do I do it?

I tried this:

JS.Class.extend({
  my_method: function() {}
});

However when I try calling from the inside of an object inherited from JSClass:

MyObject = new JS.Class({
  initialize: function() {
    this.my_method();
  }
});

it says Uncaught TypeError: Object [object Object] has no method 'call_with_self'

James Coglan

unread,
Mar 17, 2013, 4:48:57 PM3/17/13
to jsclas...@googlegroups.com
On 6 March 2013 00:17, Roman Snitko <roman....@gmail.com> wrote:
So, I have a rather useful method I want to expose to all objects of classes that inherit from JS.Class. In other words, I need to extend JS.Class with useful functionality.
How do I do it?

Sorry for the delay, I was away for a couple weeks.

All classes created with `new JS.Class({})` include a module called JS.Kernel, which includes the core object methods. Docs: 


If you need a method to exist on all objects, you should put it in a module and mix that module into Kernel:

var MyModule = new JS.Module('MyModule', {
  my_method: function() { ... }
});

JS.Kernel.include(MyModule); 
Reply all
Reply to author
Forward
0 new messages