Search code examples
javascriptrhinonashorn

Expose an object's member functions as global functions


For example I have:

var n = {
  abc: function() { print("abc"); },
  def: function() { print("def"); },
};

Is there a way to export abc and def as global functions, so that I can call abc() directly rather than n.abc()?

My context for this is using Rhino/Nashorn script engine, and I'd like to inject a "global" object that provides global functions.


Solution

  • Why don't you just use JavaScript's bind, call and apply methods to call the object's member functions when you need to invoke them? Making them global indicates code smell.