var name = 'window' function Person(name) { this.name = name this.foo1 = function () { console.log(this.name) }, this.foo2 = () => console.log(this.name), this.foo3 = function () { return function () { console.log(this.name) } }, this.foo4 = function () { return () => { console.log(this.name) } } } var person1 = new Person('person1') var person2 = new Person('person2')person1.foo1() // 结果:________6__________ person1.foo1.call(person2) // 结果:_______7___________ person1.foo2() // 结果:__________8________ person1.foo2.call(person2) // 结果:_______9___________ person1.foo3()() // 结果:_______10___________ person1.foo3.call(person2)() // 结果:________11__________ person1.foo3().call(person2) // 结果:________12__________ person1.foo4()() // 结果:________13__________ person1.foo4.call(person2)() // 结果:_______14___________ person1.foo4().call(person2) // 结果:________15__________
时间: 2024-04-26 09:23:33 浏览: 118
javascript 中关于 this 的用法.zip
1. person1.foo1() // 结果: "person1"
2. person1.foo1.call(person2) // 结果: "person2"
3. person1.foo2() // 结果: "person1"
4. person1.foo2.call(person2) // 结果: "person1"
5. person1.foo3()() // 结果: "window"
6. person1.foo3.call(person2)() // 结果: "window"
7. person1.foo3().call(person2) // 结果: "person2"
8. person1.foo4()() // 结果: "person1"
9. person1.foo4.call(person2)() // 结果: "person2"
10. person1.foo4().call(person2) // 结果: "person1"
阅读全文