您现在的位置是:
首页 >
Web前端
>
JavaScript
JavaScript
js中this的含义
2024-01-15JavaScript
js中this的含义
```
1. this 是系统内置的只读变量
2. this 的值在不同的地方是不一样的
```
### 4.2 this 的指向(取值)
```
1. 在函数外面使用(全局下使用)
this 的值是 window
2. 在构造函数内部使用
this 的值是构造函数的实例(实例化构造函数所创建的对象)
3. 在函数(方法)中使用
this 的值是调用该函数(方法)的对象
注意: 不要看函数声明语句所在的地方,看调用函数的语句,看.前面是哪个对象
```
<script>
console.log(this);
var username = this;
console.log(username);
console.log('');
var obj = {};
obj.getInfo = function() {
console.log(this);
}
obj.getInfo();
</script>
很赞哦! ()