当前位置:网站首页>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();
边栏推荐
猜你喜欢
【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
双线性插值公式推导及Matlab实现
Lecture topics and guest blockbuster, TDengine developers conference to promote data technology "broken"
不知道这4种缓存模式,敢说懂缓存吗?
机器学习中专业术语的个人理解与总结(纯小白)
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
Likou 59 - Spiral Matrix II - Boundary Judgment
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
随机推荐
2022 年值得尝试的 7 个 MQTT 客户端工具
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
CLIP论文解读
危化企业双重预防机制数字化建设进入全面实施阶段
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
利用 rpush 和 blpop 实现 Redis 消息队列
剑指 Offer II 044. 二叉树每层的最大值-dfs法
谁的孙子最多II
leetcode 326. Powers of 3
RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
Detailed explanation of JWT
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
盲埋孔PCB叠孔设计的利与弊
力扣203-移除链表元素——链表
百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
ES6简介及let、var、const区别
leetcode 461. 汉明距离
Hinton2022年RobotBrains访谈记录