简体   繁体   English

关于javascript箭头函数表达式

[英]about javascript arrow function expression

I am Korean, so I hope you could understand it with my awkward English skills我是韩国人,所以我希望你能用我笨拙的英语理解它

I am studying arrow function expressions我正在研究箭头函数表达式

and here is my question with source code这是我的源代码问题

 var arguments = [1, 2, 3]; var arr = () => arguments[0]; console.log( arr() ) function foo(n){ var f = () => arguments[0] + n console.log(arguments[0]) //5 console.log("----arguments[0]") return f(); } console.log( foo(5) ) console.log("---------------------") function fooo(n){ var f = (...arguments) => arguments[0] + n console.log(...arguments) //5 console.log("----...arguments") return f(2); } console.log( fooo(5) )

I don't get why the second function's console.log = 10 and third function's = 7 can anyone explain to me the order code process and why that output is?我不明白为什么第二个函数的 console.log = 10 和第三个函数的 = 7 谁能向我解释订单代码过程以及为什么输出? thank you.谢谢。

In JS, every conventional function has a built-in object called arguments .在 JS 中,每个常规函数都有一个名为arguments的内置对象。 However, Arrow functions do not have this built-in object.但是,箭头函数没有这个内置对象。 So, if referred to arguments from within an Arrow function, the reference automatically goes to any external variable declared by that name.因此,如果从 Arrow 函数内部引用arguments ,则该引用会自动转到由该名称声明的任何外部变量。

The result you see is due to this effect.您看到的结果是由于这种影响。 In regular function calls.在常规函数调用中。 your reference to the arguments global variable actually refers to their in-built arguments object.您对arguments全局变量的引用实际上是指它们的内置arguments对象。

The following code demonstrates this effect.下面的代码演示了这种效果。

 let arguments = 'this is a string'; function argumentsTest() { console.log(arguments); } let argumentsArrowTest = () => { console.log(arguments); } argumentsTest(10); //{0: 10} argumentsArrowTest(10); // this is a string

In an arrow function, arguments , like this , will refer to either在箭头函数中, arguments ,像this ,将指代

  • an outer identifier named arguments (though it's very unusual to call a variable arguments due to name collision), or一个名为arguments的外部标识符(尽管由于名称冲突而调用变量arguments是非常不寻常的),或者
  • the arguments provided to a full-fledged function提供给成熟function

whichever is closer, lexically.在词汇上,哪个更接近。

In foo ,foo

var f = () => arguments[0] + n

is an arrow function, so arguments refers to an outer binding.是一个箭头函数,所以arguments是指外部绑定。 The outer environment is外部环境是

function foo(n){
  var f = () => arguments[0] + n

so arguments refers to the arguments provided to foo .所以arguments是指提供给foo的参数。 Here, it's similar to doing在这里,它类似于做

function foo(n){
  const theArguments = [n];
  var f = () => theArguments[0] + n

And since the argument passed to foo is 5, that plus n (the same argument) is 10.由于传递给foo的参数是 5,所以加上n (相同的参数)是 10。

In the third function, the same logic applies.在第三个函数中,同样的逻辑适用。 Inside an arrow function, arguments refers to the closest identifier named arguments , or the closest function.在箭头函数中, arguments指的是最近的名为arguments标识符,或最近的函数。 Here, it's the parameters:这里是参数:

var f = (...arguments) => arguments[0] + n

Since f is called with a single argument, this simplifies to由于f是用单个参数调用的,这简化为

var f = (arg) => arg + n

That argument is 2, and the n is what's passed to fooo , which is 5. Add them together, you get 7.该参数为 2, n是传递给fooo ,即 5。将它们加在一起,得到 7。

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

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