35 lines
822 B
Markdown
35 lines
822 B
Markdown
# 练习 12:this
|
||
|
||
## 目标
|
||
|
||
理解 `this` 会随着调用方式不同而变化。
|
||
|
||
## 你要练什么
|
||
|
||
- 对象方法调用
|
||
- 普通函数调用
|
||
- 箭头函数继承外层 `this`
|
||
- `this` 指向
|
||
|
||
## 任务
|
||
|
||
请完成一个“学习者对象”脚本,要求:
|
||
|
||
- 创建一个带 `name` 和 `stage` 的对象
|
||
- 写一个对象方法,方法内部用 `this.name`
|
||
- 取出这个方法单独调用,观察 `this` 变化
|
||
- 再写一个返回箭头函数的方法,观察箭头函数为什么还能拿到对象的 `this`
|
||
- 输出 3 种调用结果
|
||
|
||
## 文件
|
||
|
||
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/12-this-keyword/starter.js)
|
||
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/12-this-keyword/answer.js)
|
||
|
||
function a() {
|
||
|
||
}
|
||
|
||
const b = () => {
|
||
|
||
} |