当前位置:网站首页>ES6箭頭函數的使用
ES6箭頭函數的使用
2022-07-23 22:31:00 【InfoQ】
前言
起步
function fun() {
return 100;
}
console.log(fun()); //100const fun = function() {
return 100;
}
console.log(fun()); //100const fun1 = () => {
return 100;
}
console.log(fun1()); //100const fun2 = x => x;
console.log(fun2(100)); //100- ()中定義參數,如果只有一個參數,()可以省略
- {}中寫函數體,如果函數體中只有返回值,可以不寫return
箭頭函數與普通函數的區別
舉個例子
let obj = {
name: '小明',
age: 3,
sayName() {
setTimeout(function() {
console.log("我是" + this.name);
}, 500)
}
}
obj.sayName();我是undefinedlet obj = {
name: '小明',
age: 3,
sayName() {
setTimeout(function() {
console.log(this);
}, 500)
}
}
obj.sayName();let obj = {
name: '小明',
age: 3,
sayName() {
let self = this;
setTimeout(function() {
console.log("我是" + self.name);
}, 500)
}
}
obj.sayName();我是小明使用箭頭函數
let obj = {
name: '小明',
age: 3,
sayName() {
setTimeout(() => {
console.log("我是" + this.name);
}, 500)
}
}
obj.sayName();我是小明我想你們和我都有同樣的一個疑惑:為什麼使用箭頭函數就可以實現了呢?箭頭函數與普通函數的區別
- this指向不同
- 普通函數:誰調用這個函數,
this指向誰 - 箭頭函數:在哪裏定義函數,
this指向誰
片尾
边栏推荐
- ONEFLOW V0.8.0 officially released
- University database creation and query practice -- database table design
- Tools in the tools package of Damon database (operate Damon database)
- 关于电脑端同步到手机端数据
- [learning notes] diameter and center of gravity of the tree
- DeFi项目的盈利逻辑 2021-04-26
- 糖尿病遗传风险检测挑战赛进阶
- 软件测试工作内容太简单怎么办?
- 详解NAT技术
- I, AI doctoral student, online crowdfunding research topic
猜你喜欢

Life always needs a little passion

Still worrying about xshell cracking, try tabby

02.网页结构相关知识补充

Introduction to database system fifth edition after class exercises - Chapter 1 Introduction

Excel password related

Ali onedate's layered thought

ADB 命令结合 monkey 的简单使用,超详细

Multithreading problem: why should we not use multithreading to read and write the same socket connection?

JDBC programming of MySQL

JS——事件代理和应用场景
随机推荐
Drools (1): introduction to drools
zk 是如何解决脑裂问题的
Microsoft SQL Server database language and function usage (XIII)
Can Verilog of synthetizable be integrated
[golang learning notes] concurrency basis
小说里的编程 【连载之十九】元宇宙里月亮弯弯
Altium Designer - schematic diagram of Arduino uno & PCB diagram (self-made Arduino board)
Tiktok launches multilingual subtitles and translation tools
记忆化搜索 - DP
Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first
思源笔记的字体比其他的编辑器(Atom,VSC,sublime)内字体渲染更细更淡
关于电脑端同步到手机端数据
Programming in the novel [serial 18] the moon bends in the yuan universe
STM32单片机使用ADC功能驱动手指检测心跳模块
接口测试
Array -- 209. Subarray with the smallest length
DeFi項目的盈利邏輯 2021-04-26
Jmeter性能综合实战——签到及批量签到
Memory search - DP
Linked list - 203. remove linked list elements