简体   繁体   English

什么是es6胖箭头,等同于es5函数声明

[英]what is the es6 fat arrow equivalent to a es5 function declaration

With ES5 I can declare either a function declaration or expression, depending on what I need. 使用ES5,我可以根据需要声明函数声明或表达式。

function es5FunctionDeclaration() {
  return 'I am an es5 function declaration';
}

var es5FunctionExpression = function() {
  return 'I am an es5 function expression';
}

With the ES6 fat arrow, it is common to create a function expression like this... 使用ES6粗箭头,通常会创建如下所示的函数表达式...

const es6FunctionExpression = () => {
  return 'I am an es6 function expression';
}

But I haven't find a way to do a function declaration with a fat arrow, perhaps it is not possible. 但是我还没有找到用粗箭头进行函数声明的方法,也许是不可能的。

// es6FunctionDeclarationWithFatArrow ??

To declare functions in ES6 you can do it via: 要在ES6中声明函数,您可以通过以下方法实现:

const functionName = () => {};

Or you can do it via the function keyword: 或者,您可以通过function关键字来做到这一点:

function functionName() {}

And, if you're creating ES6 classes you don't create functions but methods: 而且,如果要创建ES6类,则不会创建函数,而是创建方法:

class MyClass {
  constructor() {}
  method1() {}
  method2() {}
}

The definition of an arrow function is here: http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions 箭头功能的定义在此处: http : //www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions

ArrowFunction[In, Yield] :
    ArrowParameters[?Yield] [no LineTerminator here] => ConciseBody[?In]
ArrowParameters[Yield] :
    BindingIdentifier[?Yield] CoverParenthesizedExpressionAndArrowParameterList[?Yield]
ConciseBody[In] :
    [lookahead ≠ { ] AssignmentExpression[?In] { FunctionBody }

What this means in English, is that an arrow function can only be declared like so args => {} and that's it. 这在英语中的意思是,箭头函数只能像args => {}这样声明。 As you say, you can bind it to a variable with, for example, const func = args => {} or by passing it as an argument to another function, but there's no way. 如您所说,您可以使用const func = args => {}或将其作为参数传递给另一个函数,从而将其绑定到变量,但这是不可能的。 Indeed, passing an arrow function as an argument was one of the major reasons it was created (because of binding this ). 确实,将箭头函数作为参数传递是创建它的主要原因之一(因为绑定了this )。 See lexical this (and the appendix ) 参见词汇this (和附录

As @MaheerAli has mentioned, arrow functions neatly avoids function hoisting which may be surprising behaviour. 正如@MaheerAli所提到的,箭头函数巧妙地避免了函数提升 ,这可能是令人惊讶的行为。

ES6 does have some ways to declare functions in a shorter manner, such as within objects: ES6确实有一些以较短的方式声明函数的方法,例如在对象内:

const obj = {
    func() {
        // ...
    }
}

First of all hoisting is not a good/useful phenomena and function declarations are hoisted with value. 首先,提升不是一个好的/有用的现象,并且函数声明也具有价值。 The problem of not throwing error even declaring a variable twice is also due to hoisting. 甚至两次声明一个变量也不会抛出错误的问题也是由于提升。

ES6 was designed to remove all those problems. ES6旨在消除所有这些问题。 So there was no point to have the same function declaration again added as a new feature to cause problems again. 因此,没有必要再次添加相同的函数声明作为新功能来再次引起问题。

The is same like why in ES6 let and const can't be redeclared or how we could make let and const like var 就像为什么在ES6中不能重新声明letconst或如何使letconstvar一样

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

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