# 章序号/节序号/节/笔记序号
# 概念阐释
## 语义
`this`关键字引用对象实例的属性。
## 语法
```js
this
```
# 举例子

# 类比、比较与对比
如果不用`this`的例子:
- person1和person2的方法一摸一样,但只有`this`才能让代码指向所在的对象,所以person1会输出Chirs,person2会输出Brain
```js
var person1 = {
name : 'Chris',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
var person2 = {
name : 'Brian',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
```
在使用`this`关键字时不能使用[[JavaScript 函数#^b3b059|箭头函数]],这是因为箭头函数不是绑定`this`值,而是继承`this`值,所以在需要访问对象属性的实例中会出问题。
# 问题
- [练习题](https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/adv-this)
- 使用箭头函数和`this`关键字会怎样?
1. 让我们创建一个新对象来练习使用它。
在main.js中有一个名为`robot`的对象,添加一个属性`model`,并将其赋值为`'1E78V2'`。再添加另一个属性`energyLevel`,并将其赋值为`100`。
2. 在机器人对象内,添加一个名为`provideInfo`的方法。在`provideInfo()`的主体内,使用插值返回以下字符串:
```
I am MODEL(替换) and my current energy level is ENERGYLEVEL(替换).
```
记住,在方法内获取调用对象的属性时,必须使用this关键字!
3. 调用`provideInfo()`方法并print结果。
# 问题答案
```js
let robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo() {
return `I am ${this.model} and my current energuy level is ${this.energyLevel}.`
}
};
console.log(robot.provideInfo());
```
# 备注(经验集/错误集)
## 参考资料
[MDN-this](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this)