javascript“静态导入" [英] javascript "static imports"
问题描述
javascript中是否有任何等同于Java静态导入的内容?例如,如果我有一个Math
看起来像
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())
但是我不想将function1和function2放在全局名称空间中.这可能吗?
But I don't want to put function1 and function2 in the global namespace. Is this possible?
推荐答案
是的.称为 with
.
Yes, there is. It's called with
.
with (com.example.Math) {
function2(function1());
}
说:
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.
例如:
var m = com.example.Math;
m.function2(m.function1());
这篇关于javascript“静态导入"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!