【发布时间】:2020-05-31 12:08:51
【问题描述】:
假设我们有一个 Button 元素
const ourButton = document.getElementById("#theButton");
我们想要一个流畅的 API 来改变这个按钮的样式而不创建一个新的对象,所以像这样链接一个函数:
style(ourButton).property("padding").value("32px");
这可能吗?我似乎无法弄清楚如何创建这种行为。我尝试通过创建如下构造函数来“以传统方式”构建 Fluent API:
var FStyle = function(node) {
this.node = node;
}
FStyle.prototype.property = function(property) {
this.property = property;
return this;
}
FStyle.prototype.value = function(value) {
this.value = value;
this.node.style[this.property] = this.value;
return this;
}
并通过构造一个新对象来使用它:
const ourButtonStyle = new FStyle(ourButton);
ourButtonStyle.property("padding").value("64px");
一次有效。如果我想添加一种新样式,我必须创建一个全新的对象。这是为什么呢?
TL;DR:出于学习目的,我正在尝试链接功能,但对它的理解不够充分,无法理解上述行为。在普通函数中返回 this 以将其他函数链接到它也不会完成这项工作。最后我想将一个函数的结果“管道”到另一个函数。
【问题讨论】:
-
为什么只能运行一次?看起来
ourButtonStyle.property("foo").value("bar").property("baz").value("qux")应该可以正常工作。 (除此之外:ourButtonStyle.property("foo").property("bar").value("baz")之类的东西也是可能的,但令人困惑,这表明这可能不是最好的 API 设计。) -
只是说:创建新对象绝对没有错!
-
@Thomas 执行一次链后出现错误:“未捕获的 TypeError:ourButtonStyle.property 不是函数”。 MRA:codepen.io/melvinidema/pen/wvKVNba?editors=0011
-
@Bergi 我同意,但我为什么要这样做。 (而不仅仅是调用:
node.style.property = value- 这更简约)是练习方法链接和创建 Fluent API。每次你想改变风格时创建一个新对象在 IMO 中并不实用。 -
@MelvinIdema 在理想的 fluent API 中,所有对象都是不可变的,每个方法调用都会返回一个新实例。 (我并不是说您应该多次调用
new FStyle(…),我只是说您可以轻松地使用链中的多个对象)。
标签: javascript ecmascript-6 fluent chaining