當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
javascript - this
生活随笔
收集整理的這篇文章主要介紹了
javascript - this
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 /**
2 * this默認(rèn)指向全局
3 * 單獨(dú)執(zhí)行 -> 全局變量(一旦對(duì)象被window引用,也是成全局)
4 */
5 // console.log(this);
6
7 // 當(dāng)然->這也是指向全局的
8 function thatThis(x, y) {
9 this.x = x;
10 this.y = y;
11 }
12
13
14 // Window?{postMessage: ?, blur: ?, focus: ?, close: ?, frames: Window,?…}
15
16
17
18 /**
19 * 改變this指向
20 *
21 * 作者:LuckyJing
22 * 鏈接:https://www.jianshu.com/p/796e903754f1
23 * 來(lái)源:簡(jiǎn)書(shū)
24 * 簡(jiǎn)書(shū)著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。
25 */
26 function alterThis() {
27 var object = {
28 name: "My Object",
29 getNameFunc: function () {
30 // 改變this引用.
31 var that = this;
32 console.log(that);
33 // return function () {
34 // return that.name;
35 // };
36 }
37 };
38 }
39 // obj環(huán)境執(zhí)行 -> 本身
40 alterThis();
?
詳情見(jiàn)阮一峰this
轉(zhuǎn)載于:https://www.cnblogs.com/cisum/p/9577226.html
總結(jié)
以上是生活随笔為你收集整理的javascript - this的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: P2105 K皇后
- 下一篇: PP生产订单成本的计划、控制和结算