我知道 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/