Jeff Storey
Jeff Storey

Reputation: 57192

javascript "static imports"

Is there anything in javascript that is the equivalent of java static imports? For example, if I have a Math class that looks like

com.example.Math = function() {

   function1(...) {}
   function2(...) {}

}

Now some of these functions are naturally chained together such that the output to one is the input to another. I can do something like

com.example.Math.function2(com.example.Math.function1());

This is a little ugly looking, and I would really like to do something like:

function2(function1())

But I don't want to put function1 and function2 in the global namespace. Is this possible?

Upvotes: 3

Views: 3416

Answers (4)

4thex
4thex

Reputation: 1166

I would do something like this:

var O = function() {
  var that = {};
  var PI = Math.PI;
  that.circ = function(r) {
    return 2*PI*r;  
  };
  return that;
};
var o = O();
console.log(o.circ(1));

Notice how PI is used without the Math namespace in the O.prototype.circ method.

In JavaScript, there is no distinction between a namespace and an object, so some would argue that Math is not a namespace, but since JavaScript doesn't support the concept, it is as much a namespace as com.mycompany.somelibrary.

Upvotes: 1

OverZealous
OverZealous

Reputation: 39570

One option is to use a closure to wrap the object. It doesn't necessarily eliminate the object itself, but it helps with readability and if you are using a JS compressor can help reduce the output file size:

(function(Math) {
    Math.function2(Math.function1(...));
}(com.example.Math);)

You can also pass in multiple objects (ie: function(Math, Foo) {...}(com.example.Math, com.example.Foo)).


If you want to use just a few functions directly, just pass them in like this:

(function(function1, function2) {
    function2(function1(...));
}(com.example.Math.function1, com.example.Math.function2);)

This, however, removes the relationship between the Math instance and the functions, so you might get some weird behavior if your methods depend on instance variables. As an example of how that won't work, imagine this class:

com.example.Counter = {
    counter: 0,
    increment: function() { this.counter++; }
}

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 185913

How about:

var Math = com.example.Math;

and then:

Math.fn1( Math.fn2(...) );

I'm assuming of course that your code is not global code. (If you're not familiar with the concept of avoiding global code in JS, read about the module pattern.)


You can go one step further:

var Math = com.example.Math,
    func1 = Math.func1,
    func2 = Math.func2;

and then:

func1( func2(...) );

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359786

Yes, there is. It's called with.

with (com.example.Math) {
    function2(function1());
}

That said:

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.

For example:

var m = com.example.Math;
m.function2(m.function1());

Upvotes: 3

Related Questions