-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
35 lines (30 loc) · 727 Bytes
/
Copy paththis.js
File metadata and controls
35 lines (30 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 构造函数 this指向实例对象
function Foo () {
this.name = 'foo'
console.log('Foo this', this)
console.log('Foo this === Foo', this === Foo)
}
var objF = new Foo()
// 作为一个对象的属性
var obj = {
name: 'obj',
printName: function () {
console.log('obj this', this.name)
}
}
obj.printName()
// 普通函数中的 this
function fn () {
console.log('fn this', this)
}
fn()
// call apply this
function fn1 (arg1, arg2) {
console.log('fn1 this.name', this.name)
console.log('arg1, arg2', arg1, arg2)
console.log('fn1 this', this)
}
fn1.call({ name: 'call' }, 'call1', 'call2')
fn1.apply({ name: 'apply' }, ['apply1', 'apply2'])
var fn2 = fn1.bind({ name: 'bind' })
fn2('fn2', 'fn2')