admin管理员组文章数量:1415673
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
4 Answers
Reset to default 3Yes, 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++; }
}
本文标签: namespacesjavascript quotstatic importsquotStack Overflow
版权声明:本文标题:namespaces - javascript "static imports" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745236247a2649054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论