使用getter和setter实现函数链接的方法
Approach to function chaining with getters and setters
如果我想使用fluent函数链来支持对象属性的分配。例如:
foo.width(500).height(250).margin({left:5,right:10});
我显然可以创建一个函数定义,比如:
margin(value) {
this.config.margin = value;
return this;
}
但是,如果我想能够支持上面的函数链接,但也直接赋值,比如:
foo.margin = {left:5,right:10};
我可以通过添加这样的设置器来添加这种支持:
set margin(value) {
this.config.margin = value;
}
但是,不能有同名的setter和函数,显然setter只适用于文字赋值操作,函数定义只适用于流畅的API方法。
有没有一种方法可以在JS ES6中以一种语法优雅的方式实现这两者?
我已经包含了一个fiddle,它演示了一个流利和字面赋值运算符的工作示例。唯一的问题是什么?我不得不使用不同的命名签名,这增加了API表面。。。如果可能的话,我想避免这种情况。
http://www.es6fiddle.com/i6o0jscx/
如果您愿意使用额外的两个字符来检索属性值,那么您可以这样做:
export class Foo {
constructor() {
this.config = {
width:500,
height: 400
};
}
get width() {
return function(value) {
if (arguments.length) {
this.config.width = value;
return this;
}
return this.config.width;
};
}
set width(value) {
this.config.width = value;
}
}
let foo = new Foo();
console.log(foo.width());
foo.width = 600;
console.log(foo.width());
console.log(foo.width(250).width());
基本上,getter返回一个函数,如果使用参数调用它,则该函数设置值;如果不使用参数调用,则返回值。这与API jQuery为.text()
和.html()
以及许多其他内容提供的类似,但它为您提供了直接分配给属性的附加选项。我真的不建议这样做,因为能够做foo.width = 5;
而不能做var w = foo.width;
会让人困惑,但我看不到一个好的方法来完全实现你想要做的事情。
http://www.es6fiddle.com/i6o14n4b/
两者都可以。您只需要遵守命名约定:
class Thing {
constructor() {
this._property = 0;
}
get property() {
return this._property;
}
setProperty(property) {
this.property = property;
return this;
}
set property(property) {
this.setProperty(property);
}
}
这样你既可以吃蛋糕也可以吃。
相关文章:
- 在新选项卡中打开链接,但不使用_blank方法
- 简单的JavaScript方法链接
- 当用户点击动态创建的链接时,如何调用JS方法.JQuery,ASP.NET MVC
- 有没有一种方法可以让链接不可点击,但仍然可以使用:悬停
- D3使用方法链接时的不同行为
- 如何在不链接/jquery的情况下使用方法应用css属性数组
- 有没有一种方法可以创建到同位素过滤器的直接链接
- JavaScript - 创建可链接函数时的最佳方法是什么
- 什么'这是使用jQuery将一些文本锚定到外部链接的最简单方法
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 如何在grails中链接javascript引导程序方法
- 将$ionicLoading.show()用于外部链接的方法
- 在coffeescript中链接jquery方法和参数
- 使用getter和setter实现函数链接的方法
- JS:将元素附加到 head - 如何链接方法
- JavaScript可链接方法Delima
- 将元素作为对象返回的角度定向链接方法
- 用jquery链接方法
- 来自字符串的链接方法
- 按需链接方法