简体   繁体   English

JavaScript中的菊花链方法

[英]Daisy chaining methods in JavaScript

This came to may mind over the weekend and I was curious to know exactly what is happening when one chains methods together like the below code: 周末可能会想到这一点,而我很好奇,当一个方法链接在一起时,如下面的代码所示,我确切地知道正在发生什么:

const posts = [{}, {}, {}, ...]

posts
  .filter(({ node }) => node.fields.slug !== '/about/')
  .reverse()
  .slice(0, 5)

Each of the Array.prototype methods returns an array after transforming the data in some fashion. 每个Array.prototype方法在以某种方式转换数据后都会返回一个数组。 filter & slice are both returning copies of the original array with the prescribed transformation present. filterslice都是返回带有指定转换的原始数组的副本 What I am curious to know is if JavaScript is performing some internal piping like I would see in elixir . 我很好奇的是,JavaScript是否正在执行一些内部管道,如我在elixir看到的那样。 Each method requires an array to work, but not necessarily as an argument, I think. 我认为每种方法都需要一个数组才能工作,但不一定必须作为参数。

I don't believe the above is similar to. 我不认为以上类似。 I know that the first arg must be whatever is returned from the prior operation for the pipe operator to work in elixir : 我知道第一个arg必须是先前操作返回的值,管道操作员才能在elixir工作:

posts
  |> filter(({ node }) => node.fields.slug !== '/about/')
  |> reverse()
  |> slice(0,5)

Wasn't sure where to look to better understand this so if you can point me in the right direction I'll go read up on it. 不知道在哪里可以更好地理解这一点,因此,如果您可以向我指出正确的方向,我会继续阅读下去。 I was just curious to know, thanks so much for the information. 我只是想知道,非常感谢您提供的信息。

It's nothing more complicated than returning either this or some other new object. 没有什么比返回this对象或其他新对象更复杂的了。 A simple example would be: 一个简单的例子是:

 function Num(val) { // Num contructor this.val = val; }; Num.prototype.increment = function () { this.val++; return this; } Num.prototype.decrement = function () { this.val--; return this; } Num.prototype.getVal = function(){ return this.val; } var num = new Num(0); num.increment().increment().decrement().increment(); console.log(num.getVal()); // 2 

The non-chaining way would be: 非链接方式为:

var num = new Num(0);
num.increment();
num.increment();
num.decrement();
num.increment();
console.log(num.getVal()); // 2

A good reason to use chaining is when the method returns a new immutable object (eg string) but you don't care about the intermediary objects, you just want the final result: 使用链接的一个很好的理由是当该方法返回一个新的不可变对象(例如字符串),但是您不关心中间对象时,您只需要最终结果:

var username = " Dave"; // user input with leading whitespace
var lowercase = username.trim().toLowerCase(); // "dave"

Not using chaining in this case wouldn't produce the desired result (no whitespace): 在这种情况下,不使用链接不会产生预期的结果(没有空格):

var username = " Dave"; // user input with leading whitespace
username.trim();
var lowercase = username.toLowerCase(); // " dave"

You could still avoid chaining though if you reassigned the return value: 如果您重新分配了返回值,您仍然可以避免链接:

var username = " Dave"; // user input with leading whitespace
var lowercase = username.trim();
lowercase = lowercase.toLowerCase(); // "dave"

And just for completeness, if you wanted to make the above Num example immutable: 为了完整起见,如果您想使上述Num示例不可变:

Num.prototype.increment = function () {
    return new Num(this.val + 1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM