let obj = {
myName: '张三',
age: '11',
run: function () {
console.log(this.myName + '正在跑步');
console.log(obj.myName + '正在跑步');
}
}
obj.run();
let person = {
name: 'zhangsan',
}
function fn1(content) {
return content?.name.toUpperCase();
}
function fn(content) {
fn1(content);
}
fn(person);
function fun1() {
console.log("this", this)
return this.name.toUpperCase();
}
function fun() {
const result = fun1.call(this);
console.log("result", result)
}
fun.call(person);
let obj2 = {
a: 1,
bar: bar
}
function bar() {
console.log("bar=》this", this)
}
bar()
obj2.bar()
bar.call(obj2)
function foo1(x, y) {
console.log("显示绑定传参", this, x, y)
}
foo1.call(obj2, 1, 2)
foo1.apply(obj2, [1, 2])
var obj3 = {
a: 100,
b: function () {
const fn = () => {
console.log("new=>this", this.a);
}
function fn1() {
console.log("new=>this", this.a);
}
fn()
fn1()
fn1.call(this)
fn1.call(obj3)
}
}
obj3.b()
参考: juejin.cn/post/738649…