Javascript 中 with 语句的未来

发布于 2024-10-23 19:17:30 字数 785 浏览 4 评论 0原文

我知道不建议使用 with 语句 在 Javascript 中,在 ECMAScript 5 中被禁止,但它允许人们在 Javascript 中创建一些不错的 DSL。

例如 CoffeeKup 模板引擎和 Zappa 网络 DSL。这些使用了一些非常 奇怪的使用with语句实现DSLish的作用域方法对他们的感觉。

with 语句和这些类型的 DSL 有未来吗?

如果没有 with 语句,可以实现这种 DSL 效果吗?

I know that usage of the with-statement is not recommended in Javascript and is forbidden in ECMAScript 5, but it allows one to create some nice DSLs in Javascript.

For example CoffeeKup-templating engine and the Zappa web DSL. Those uses some very weird scoping methods with the with-statement to achieve DSLish feeling to them.

Is there any future with the with-statement and these kinds of DSLs?

Can this DSL-effect be achieved without the with-statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伤痕我心 2024-10-30 19:17:30

在coffeescript中,有一个很好的技巧可以在不使用with的情况下继续使用花哨的dsls:

 using = (ob, fn) -> fn.apply(ob)

 html = 
   head : (obj) -> # implementation
   body : (fn)  -> # implementation
   div  : (str) -> # implementation

 using html, ->
   @head
     title: "My title"
   @body =>
     @div "foo bar"
     @div "qux moo"

 /*
   in pure javascript you'd be using
   with(html){
     head({title:"My title"});
     body(function(){
       div("foo bar");
       div("qux moo");
     });
   }
 */

In coffeescript, there is a nice trick to keep using fancy dsls without using with:

 using = (ob, fn) -> fn.apply(ob)

 html = 
   head : (obj) -> # implementation
   body : (fn)  -> # implementation
   div  : (str) -> # implementation

 using html, ->
   @head
     title: "My title"
   @body =>
     @div "foo bar"
     @div "qux moo"

 /*
   in pure javascript you'd be using
   with(html){
     head({title:"My title"});
     body(function(){
       div("foo bar");
       div("qux moo");
     });
   }
 */
じее 2024-10-30 19:17:30

ECMAScript 5 中“禁止” 是一种常见的误解

仅在 ECMAScript 5 的严格模式下(请注意,这是选择加入)- with 语句是语法错误。因此,您当然仍然可以在完全符合 ECMAScript 5 的实现中使用 with,只要它们出现在非严格(或 Crockford 所说的草率)代码中即可。它对于性能来说并不是很好(因为 with 的存在常常会杀死现代引擎中的各种优化),但它会起作用。

ECMAScript 的未来版本很可能基于严格模式行为,尽管也可能是选择加入的。因此,当涉及到未来的脚本验证时,遵守严格模式无疑是一个好主意。

with being "forbidden" in ECMAScript 5 is a common misconception.

Only in strict mode of ECMAScript 5 — which is opt-in, mind you — with statement is a syntax error. So you can certainly still use with in fully ECMAScript 5 -compliant implementations, as long as they occur in non-strict (or sloppy, as Crockford calls it) code. It won't be pretty for performance (since mere presence of with often kills various optimizations in modern engines) but it will work.

Future versions of ECMAScript are very likely to be based on strict mode behavior, although will also likely be opt-in as well. So conforming to strict mode is certainly a good idea when it comes to future proofing your scripts.

初见你 2024-10-30 19:17:30

为什么不直接分配一个 var 来指向该对象而不是使用 with 呢?

'with' 样式:

with(a_long_object_name_that_is_bloated) {
  propertyA = 'moo';
  propertyB = 'cow';
}

'var' 样式:

var o = a_long_object_name_that_is_bloated;
o.propertyA = 'moo';
o.propertyB = 'cow';

Why not just assign a var to point to the object instead of using with?

'with' style:

with(a_long_object_name_that_is_bloated) {
  propertyA = 'moo';
  propertyB = 'cow';
}

'var' style:

var o = a_long_object_name_that_is_bloated;
o.propertyA = 'moo';
o.propertyB = 'cow';
櫻之舞 2024-10-30 19:17:30

要回答 Epeli 的问题,请查看 CoffeeMugg,它的功能与 CoffeeKup 的功能相同,但使用了 Adrien 的技术。它使用 this. 而不是 with 语句。

To answer Epeli's question, take a look at CoffeeMugg which does what CoffeeKup does but using Adrien's technique. It uses this. instead of the with statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文