this
- 全局模式和函数下,指的对象是this。
- 在 ‘use strict’ 下是undefined
function show(){
console.log(this);
}
show()
------------------------------------------------------------
'user strict'
function show(){
console.log(this);
}
show()
- 对象this -> {}
'user strict'
var teacer = {
name:'leo',
showName:function(){
function testThis(){
console.log(this.name);
}
testThis();
}
teacer.showName();
-------------------------------------------------------
name = 'jesslyn';
var teacer = {
name:'leo',
showName:function(){
function testThis(){
console.log(this.name);
}
testThis();
}
teacer.showName();
这个时候如果要打印leo怎么办呢?以下提供了三种方法: a. let接一下 b. 箭头函数 c. bind apply call
name = 'jesslyn';
var teacer = {
name:'leo',
showName:function(){
let that = this;
function testThis(){
console.log(this.name);
}
testThis();
}
teacer.showName();
----------------------------------------------------
name = 'jesslyn';
var teacer = {
name:'leo',
showName:function(){
function testThis(){
console.log(this.name);
}.bind(this);
testThis();
}
teacer.showName();
---------------------------------------------------------
function show(a,b){
console.log(this,12,5)
}
show.call(66666.12,5)
show.apply(66666,[12,5])
参考视频 https://www.bilibili.com/video/BV15x411f7xe?p=14
|