Skip to content

Commit d5c5778

Browse files
this
1 parent 0f85ec3 commit d5c5778

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

Note/this/this.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// 普通调用, 非严格模式指向 global, 严格模式指向 undefined
2+
3+
(function(){
4+
function foo() {
5+
return this;
6+
}
7+
function bar() {
8+
'use strict'
9+
return this;
10+
}
11+
const obj = {
12+
toString() {
13+
return 'obj'
14+
},
15+
a: 1,
16+
fn() {
17+
return this;
18+
},
19+
b: {
20+
toString() {
21+
return 'obj.b';
22+
},
23+
c: 1,
24+
d() {
25+
return this;
26+
}
27+
},
28+
}
29+
const fn = obj.fn;
30+
console.log(`${foo()} ${bar()} ${fn()} ${obj.fn()} ${obj.b.d()}`);
31+
// [object global] undefined [object global] obj obj.b
32+
})();
33+
34+
35+
// 一般构造函数, 指向 new 出来的对象
36+
(function(){
37+
function P1(name) {
38+
this.name = name;
39+
}
40+
// 此时, this 指向 new 出的对象
41+
const p1 = new P1('P1');
42+
console.log(p1);
43+
// P1 { name: 'P1' }
44+
45+
function P2(name) {
46+
this.name = name;
47+
const obj = {
48+
n: name
49+
};
50+
return obj;
51+
}
52+
// 此时 this 也指向 new 出的对象, 但是由于返回值是一个对象, 则表达式 new P2('P2') 返回的并非 new 出的对象,而是 obj
53+
const p2 = new P2('P2');
54+
console.log(p2);
55+
// { n: 'P2' }
56+
57+
function P3(name){
58+
this.name = name;
59+
return 1;
60+
// return name;
61+
}
62+
// 当返回值是基本类型时, 表达式返回结果仍为 new 出的对象
63+
const p3 = new P3('P3');
64+
console.log(p3);
65+
// P3 { name: 'P3' }
66+
})()
67+
68+
69+
70+

Note/this/this.md

+72
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,78 @@
1515
* 一般由某对象调用,则绑定到该对象上,(如:obj.fn())
1616
* 箭头函数则是根据其外层上下文确定
1717

18+
### 实例代码
19+
20+
#### 直接使用
21+
**在执行函数时,若函数被上级对象所调用,则 this 指向上级对象,否则非严格模式下指向全局,严格模式下指向 undefined**
22+
23+
```js
24+
function foo() {
25+
return this;
26+
}
27+
function bar() {
28+
'use strict'
29+
return this;
30+
}
31+
const obj = {
32+
toString() {
33+
return 'obj'
34+
},
35+
a: 1,
36+
fn() {
37+
return this;
38+
},
39+
b: {
40+
toString() {
41+
return 'obj.b';
42+
},
43+
c: 1,
44+
d() {
45+
return this;
46+
}
47+
},
48+
}
49+
const fn = obj.fn;
50+
console.log(`${foo()} ${bar()} ${fn()} ${obj.fn()} ${obj.b.d()}`);
51+
// [object global] undefined [object global] obj obj.b
52+
```
53+
54+
#### 构造函数
55+
56+
**构造函数中的 this,指向被new 出的实例,但若构造函数中返回了一个对象,则会导致 new 表达式结果为该对象**
57+
58+
```js
59+
function P1(name) {
60+
this.name = name;
61+
}
62+
// 此时, this 指向 new 出的对象
63+
const p1 = new P1('P1');
64+
console.log(p1);
65+
// P1 { name: 'P1' }
66+
67+
function P2(name) {
68+
this.name = name;
69+
const obj = {
70+
n: name
71+
};
72+
return obj;
73+
}
74+
// 此时 this 也指向 new 出的对象, 但是由于返回值是一个对象, 则表达式 new P2('P2') 返回的并非 new 出的对象,而是 obj
75+
const p2 = new P2('P2');
76+
console.log(p2);
77+
// { n: 'P2' }
78+
79+
function P3(name){
80+
this.name = name;
81+
return 1;
82+
// return name;
83+
}
84+
// 当返回值是基本类型时, 表达式返回结果仍为 new 出的对象
85+
const p3 = new P3('P3');
86+
console.log(p3);
87+
// P3 { name: 'P3' }
88+
```
89+
1890

1991

2092

0 commit comments

Comments
 (0)