摆脱 CoffeeScript 的闭包包装器
如何省略在全局范围内隐藏变量的自动闭包包装器?
(function() {
// my compiled code
}).call(this);
只是玩弄 CoffeeScript+SproutCore,当然,我更愿意保持范围不变:在这种情况下,不需要保护任何内容不被覆盖。
我知道我可以在声明中使用 @
或 this.
,但这不太优雅。
How can I omit the automatic closure wrappers that hides my variables from the global scope?
(function() {
// my compiled code
}).call(this);
Just playing around with CoffeeScript+SproutCore, and of course, I'd prefer to leave the scope as it is: in this case there is no need to protect anything from overwriting.
I know I can use @
or this.
at the declaration, but that's not too elegant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速但肮脏的解决方案:使用控制台标志
-b
(裸)。 警告:如果你这样做,小猫会死!干净的解决方案:不要这样做。
Quick and dirty solution: Use the console flag
-b
(bare). Warning: Kittens will die if you do that!Clean solution: Don't do that.
我使用了另一个选项,即将全局变量附加到函数范围内的全局对象。我把我的贴在“窗户”上。这使您的 JavaScript 保持封装,并且仅在全局范围内公开您需要的变量。
I used another option which was to attach my global variables to the global object in the scope of my function. I attached mine to the 'window'. This keeps your JavaScript encapsulated and only exposes the variable that you need in the global scope.