javascript - Javascript 中 with 语句的 future

标签 javascript dsl coffeescript ecmascript-5

我知道 with 语句的用法 is not recommended在 Javascript 中并且在 ECMAScript 5 中被禁止,但它允许人们在 Javascript 中创建一些不错的 DSL。

例如CoffeeKup -模板引擎和Zappa网络 DSL。那些使用一些very weird使用 with 语句对方法进行范围界定,以实现对它们的 DSLish 感觉。

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

如果没有 with 语句能否实现这种 DSL 效果?

最佳答案

在 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");
     });
   }
 */

关于javascript - Javascript 中 with 语句的 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373577/

相关文章:

regex - 空格和哈希 (#) 作为 coffeescript 正则表达式导致错误?

javascript - 使用指令或 ng-click 更改 AngularJS 中的预览

java - 是否有适用于 Java 的 Harel Statechart DSL 工具?

java - Groovy 脚本沙箱 : Use Groovy TimeCategory syntax from Java as String

dsl - 有 AWS EC2 的 dsl 吗?

javascript - jQuery 事件气泡

node.js - 我可以在 Heroku 中运行 CoffeeScript 吗?

javascript - 为什么这个静态属性未定义?如何在 Javascript 中访问静态属性

javascript - 与服务器端和客户端验证冲突

javascript - Bootstrap 模式启动递增时间