javascript JavaScript箭头函数 - ES6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript箭头函数 - ES6相关的知识,希望对你有一定的参考价值。

/*
Function Declaration vs Function Expresssion vs Arrow Function
function declaration:
*/
function sayName(){
  console.log('My name is', name);
}
/*
function expresssion
*/
const sayName = function (){
  console.log('My name is', name);
}
/*
arrow function: remove 'function' text and then add an arrow; if it has one arg, you can omit (); if body is one line long you can omit the curly braces and 'return' text. 
*/
const sayName = () => {
  console.log('My name is', name);
}
const square = (x) => {
  return x * x;
}
/* or */
const square = x => x * x;

//---------------------------------------------
//These are all functionally the same:
//Function Declaration

function divide1(a, b) {
  return a / b; 
}

//Function Expression

const divide2 = function(a, b) {
  return a / b;
}

//Arrow Function Expression

const divide3 = (a, b) => {
  return a / b;
}

//Arrow Function Expression - concise

const divide4 = (a, b) => a / b;

以上是关于javascript JavaScript箭头函数 - ES6的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript箭头函数问题,语法问题

轻松学习 JavaScript——第 6 部分:JavaScript 箭头函数

javascript JavaScript箭头函数表达式实体

javascript JavaScript箭头函数 - ES6

javascript 箭头函数JavaScript

JavaScript箭头函数中的this详解