我知道使用with-陈述不推荐 https://developer.mozilla.org/en/JavaScript/Reference/Statements/with在 Javascript 中,并且在 ECMAScript 5 中被禁止,但它允许人们在 Javascript 中创建一些不错的 DSL。
例如咖啡库普 http://coffeekup.org/- 模板引擎和Zappa https://github.com/mauricemach/zappa网络DSL。那些使用一些very https://github.com/mauricemach/coffeekup/blob/bd1199258221d21c432009bda9e5d35ee3daa010/lib/coffeekup.coffee#L143 weird https://github.com/mauricemach/zappa/blob/5ccf1cb77a6cf258d6093a8ee97132560834e5a5/lib/zappa.coffee#L417范围界定方法with- 给他们带来DSLish感觉的声明。
有没有未来with-声明和这些类型的 DSL?
这种 DSL 效果能否在没有with-陈述?
在coffeescript中,有一个很好的技巧可以继续使用花哨的dsls而不使用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");
});
}
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)