当前位置:网站首页>ES6 - Arrow Functions
ES6 - Arrow Functions
2022-08-03 20:14:00 【Hey………】
6. Arrow function (new syntax)
The new way to define functions in ES6
//Fixed syntax structure:() => {}
Arrow functions use simplified function definition syntax
const fn = () => {console.log(123); //123}fn();
Feature 1. There is only one code in the function body, and the execution result of the code is the return value, you can omit the braces (see example below):
function func(a, b){return a + b;}console.log(func(1,2)); //3//arrow function:const func = (a, b) => a + b;console.log(func(1,2)); //3
Feature 2. If there is only one form parameter, you can omit the parentheses
function func(a){return a;}console.log(func(1)); //1//arrow function:const func = b => { //const func = b => b;return b;};console.log(func(2)); //2
Feature 3. Arrow function does not bind this keyword, this in arrow function points to the location where function is definedContext this.
function fn(){console.log(this); //{name: 'Xiao Ming'}return() => {console.log(this); //{name: 'Xiao Ming'}}}const obj = {name: 'Xiao Ming'}const resFn = fn.call(obj);resFn();
Another example:
var age = 10;var obj = {age: 20,say:() => {alert(this.age); //10//Pointing to the age object under the widget cannot generate a scope, the say method is actually defined in the global scope}}obj.say();
边栏推荐
猜你喜欢
随机推荐
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
数学之美 第六章——信息的度量和作用
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
双线性插值公式推导及Matlab实现
8.3模拟赛总结
百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
若依集成easyexcel实现excel表格增强
谁的孙子最多II
(十六)51单片机——红外遥控
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
第三方验收测试报告有什么作用?如何获取权威软件测试报告?
tensorflow-gpu2.4.1安装配置详细步骤
In-depth understanding of JVM-memory structure
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
详解AST抽象语法树
EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
【飞控开发高级教程4】疯壳·开源编队无人机-360 度翻滚