导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

JS 参考手册

JS 参考手册(字母排序)

JavaScript

JS Arrays JS Boolean JS Classes JS Dates JS Error JS Global JS Iterators JS JSON JS Maps JS Math JS Numbers JS Objects JS Operators JS Assignment JS Arithmetic JS Comparison JS Logical Operators JS Bitwise Operators JS Misc Operators JS Precedence JS Promises JS RegExp Patterns JS RegExp Reference JS Sets JS Statements JS Strings

JavaScript 箭头函数运算符


常规功能:

let result = add(1,2);

function add(a, b) {
  return a + b;
}
亲自试一试 »

箭头函数:

const add = (a, b) => a + b;

let result = add(1,2);
亲自试一试 »

描述

箭头函数运算符 (=> ) 使用简洁的语法创建函数。

功能 常规函数 箭头函数
语法 function add(a, b) {
  return a + b;
}
const add = (a, b) => a + b;
this this 是动态的
取决于函数的调用方式
this 是词法的
继承自周围的作用域
new new myFunction 可以使用 new 不能使用
hoisting(提升) 会被提升 必须在使用前定义
arguments(参数) 具有一个argument对象 必须使用剩​​余参数(...args)
使用场景 当你需要自定义 this、hoisting(提升)或arguments(参数)时 当你需要简洁的函数和词法化的 this 时

更多示例

示例

带单个参数的箭头函数(括号可以省略):

const square = x => x * x;

let result = square(2);
亲自试一试 »

示例

无参数箭头函数:

const greet = () => "Hello!";

let result = greet();
亲自试一试 »


浏览器支持

=> 是 ECMAScript 6 (ES6 2015) 的一项特性。

JavaScript 2015 自 2017 年 6 月起,所有浏览器均已支持 JavaScript 2015。

Chrome
51
Edge
15
Firefox
54
Safari
10
Opera
38
2016年5月 2017年4月 2017年6月 2016年9月 2016年6月

Copyright ©2020-2026 freew3c.com All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。