最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

namespaces - javascript "static imports" - Stack Overflow

programmeradmin1浏览0评论

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

.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

.example.Math.function2(.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?

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

.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

.example.Math.function2(.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?

Share Improve this question asked Jan 14, 2013 at 22:50 Jeff StoreyJeff Storey 57.3k75 gold badges243 silver badges413 bronze badges 1
  • Are those functions static methods of Math? – Šime Vidas Commented Jan 14, 2013 at 22:53
Add a ment  | 

4 Answers 4

Reset to default 3

Yes, there is. It's called with.

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

That said:

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

For example:

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

How about:

var Math = .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 = .example.Math,
    func1 = Math.func1,
    func2 = Math.func2;

and then:

func1( func2(...) );

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 .mypany.somelibrary.

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 pressor can help reduce the output file size:

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

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


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

(function(function1, function2) {
    function2(function1(...));
}(.example.Math.function1, .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:

.example.Counter = {
    counter: 0,
    increment: function() { this.counter++; }
}
发布评论

评论列表(0)

  1. 暂无评论