91美食网
您的当前位置:首页JavaScriptDSL流畅接口(使用链式调用)实例_javascript技巧

JavaScriptDSL流畅接口(使用链式调用)实例_javascript技巧

来源:91美食网


认真研究了一会DSL,发现了这么几件有趣的事,JavaScript用得最多的一个东西怕是链式调用 (方法链,即Method Chaining)。 有意思的是Martin Flower指出:
代码如下:
I've also noticed a common misconception - many people seem to equate fluent interfaces with Method Chaining. Certainly chaining is a common technique to use with fluent interfaces, but true fluency is much more than that.

很多人将链式调用等同于流畅接口。然而链式调用是流畅接口的一种常用方法,真实的流畅接口不止这么一点点。

DSL 流畅接口

流畅接口的初衷是构建可读的API,毕竟代码是写给人看的。

类似的,简单的看一下早先我们是通过方法级联来操作DOM
代码如下:
var btn = document.createElement("BUTTON"); // Create a

显示全文