简体   繁体   English

有人可以帮我理解这段 Javascript 代码吗?

[英]Can someone help me understand this piece of code in Javascript?

var curryIt = function(uncurried) {
  var parameters = Array.prototype.slice.call(arguments, 1);
  return function() {
    return uncurried.apply(this, parameters.concat(
      Array.prototype.slice.call(arguments, 0)
    ));
  };
};
var greeter = function(greeting, separator, emphasis, name) {
  console.log(greeting + separator + name + emphasis);
};
var greetHello = curryIt(greeter, "Hello", ", ", ".");
greetHello("Heidi"); //"Hello, Heidi."
greetHello("Eddie"); //"Hello, Eddie."

I get the overall picture of what is happening but I do not understand what is being carried out in the curryIt function.我了解正在发生的事情的总体情况,但我不明白 curryIt 函数中正在执行的操作。

Every function has an object called arguments , which contains in an array like data structure the arguments which the caller of the function uses.每个函数都有一个名为arguments的对象,它在一个类似数据结构的数组中包含函数调用者使用的参数。

For example, let that we have the following function:例如,假设我们有以下函数:

function sum(a,b){
    return a+b;
}

If we call sum as below:如果我们调用sum如下:

sum(3,4)

the arguments would contain two items 3 and 4. (Actually, you could call sum with 3, 4 or more arguments. All these values would be contained in the arguments). arguments将包含两个项目 3 和 4。(实际上,您可以使用 3、4 或更多参数调用sum 。所有这些值都将包含在参数中)。

The key word in the above statement is the array like data structure .上面语句中的关键词类似数组的数据结构 arguments is not an array. arguments不是数组。 So you can't have available the Array's methods (push, shift, slice, etc.)所以你不能使用 Array 的方法(push、shift、slice 等)

What does slice ?什么是slice

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). slice() 方法将数组的一部分的浅拷贝返回到从开始到结束(不包括结束)选择的新数组对象中。 The original array will not be modified.不会修改原始数组。

For further info please have a look here.有关更多信息,请查看此处。

So if you want to apply slice on arguments would could you do?所以,如果你想在argumentsapply切片,你能做吗?

Since arguments is not an array, ( arguments instanceof Array returns false), you can't do so like below:由于arguments不是数组,( arguments instanceof Array返回 false),您不能像下面那样这样做:

var a = ["zero", "one", "two", "three"];
var sliced = a.slice(1,3);

But you can't do it like below:但是你不能像下面那样做:

Array.prototype.slice.call(arguments, 1);

What does call?叫什么?

The call() method calls a function with a given this value and arguments provided individually. call() 方法使用给定的 this值和单独提供的参数调用函数。

For further info please have a look here .如需更多信息,请查看此处

So, essentially, the following line of code所以,基本上,下面的代码行

Array.prototype.slice.call(arguments, 1);

calls the function called slice on the arguments objects passing 1 as it's argument.在传递 1 作为arguments对象上调用名为slice的函数。 So you get an array with all the arguments except the first.所以你得到一个包含除第一个参数之外的所有参数的数组。

This is all about a functional programming paradigm called Currying.这完全是关于称为 Currying 的函数式编程范式。 It's all about saving one or more arguments into a returned function to be re used at a later time.这一切都是关于将一​​个或多个参数保存到返回的函数中以供以后重新使用。 It's highly related with the closures topic of JavaScript.它与 JavaScript 的闭包主题高度相关。

Let's refactor the code in a more functional manner.让我们以更实用的方式重构代码。

 var curryIt = (uncurried,...args) => name => uncurried(...args,name), greeter = (greeting, separator, emphasis, name) => console.log(greeting + separator + name + emphasis), greetHello = curryIt(greeter, "Hello", ", ", "."); greetHello("Heidi"); //"Hello, Heidi." greetHello("Eddie"); //"Hello, Eddie."

curryIt function takes several argument of which the first one is a function called uncurried the rest of the argument are collected in the args array by the help of the rest operator ( ... ) of ES6. curryIt函数接受多个参数,其中第一个是一个名为uncurried的函数,其余参数通过 ES6 的 rest 运算符 ( ... ) 收集在args数组中。

Then we return a function which takes a single argument called name and makes use of the parameters passed to it's parent function by args array.然后我们返回一个函数,它接受一个名为name的参数,并利用args数组传递给它的父函数的args So, right now the args array and it's elements are under closure.所以,现在args数组及其元素正在关闭。 The returned function will invoke the passed uncurried function by providing the available arguments in a proper order.返回的函数将通过以适当的顺序提供可用参数来调用传递的非uncurried函数。

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

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