整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          JavaScript this 關(guān)鍵字

          JavaScript this 關(guān)鍵字

          avaScript this 關(guān)鍵字

          面向?qū)ο笳Z言中 this 表示當(dāng)前對(duì)象的一個(gè)引用。

          但在 JavaScript 中 this 不是固定不變的,它會(huì)隨著執(zhí)行環(huán)境的改變而改變。

          在方法中,this 表示該方法所屬的對(duì)象。

          如果單獨(dú)使用,this 表示全局對(duì)象。

          在函數(shù)中,this 表示全局對(duì)象。

          在函數(shù)中,在嚴(yán)格模式下,this 是未定義的(undefined)。

          在事件中,this 表示接收事件的元素。

          類似 call() 和 apply() 方法可以將 this 引用到任何對(duì)象。

          方法中的 this

          在對(duì)象方法中, this 指向調(diào)用它所在方法的對(duì)象。

          在上面一個(gè)實(shí)例中,this 表示 person 對(duì)象。

          fullName 方法所屬的對(duì)象就是 person。

          單獨(dú)使用 this

          單獨(dú)使用 this,則它指向全局(Global)對(duì)象。

          在瀏覽器中,window 就是該全局對(duì)象為 [object Window]:

          如果單獨(dú)使用,this 也是指向全局(Global)對(duì)象。

          函數(shù)中使用 this(默認(rèn))

          在函數(shù)中,函數(shù)的所屬者默認(rèn)綁定到 this 上。

          在瀏覽器中,window 就是該全局對(duì)象為 [object Window]:

          函數(shù)中使用 this(嚴(yán)格模式)

          嚴(yán)格模式下函數(shù)是沒有綁定到 this 上,這時(shí)候 this 是 undefined。

          事件中的 this

          在 HTML 事件句柄中,this 指向了接收事件的 HTML 元素:

          對(duì)象方法中綁定

          下面實(shí)例中,this 是 person 對(duì)象,person 對(duì)象是函數(shù)的所有者:

          * this.firstName 表示 this (person) 對(duì)象的 firstName 屬性。

          顯式函數(shù)綁定

          在 JavaScript 中函數(shù)也是對(duì)象,對(duì)象則有方法,apply 和 call 就是函數(shù)對(duì)象的方法。這兩個(gè)方法異常強(qiáng)大,他們?cè)试S切換函數(shù)執(zhí)行的上下文環(huán)境(context),即 this 綁定的對(duì)象。

          在下面實(shí)例中,當(dāng)我們使用 person2 作為參數(shù)來調(diào)用 person.fullName 方法時(shí), this 將指向 person2, 即便它是 person的方法。

          相信很多jquery使用者在都很熟悉this和$(this),但是二者到底有什么區(qū)別呢,下面我們就利用代碼談?wù)劧叩膮^(qū)別。

          請(qǐng)看下圖中代碼 saveRule(this)方法中的this

          obj就是this。

          測試結(jié)果如下圖:

          總結(jié):this就是一個(gè)html元素,他沒有parent方法,$(this)是一個(gè)jquery對(duì)象,他有parent方法。

          6 - 27 - 箭頭函數(shù):基礎(chǔ) & this 關(guān)鍵字

          原文:https://dev.to/bhagatparwinder/arrow-function-basics-34cm

          簡介

          箭頭函數(shù)是在 ES6 引入的,相對(duì)于函數(shù)表達(dá)式來說是一種更簡潔的方式。

          箭頭函數(shù)名稱的來源是因?yàn)槭褂昧?=>

          語法

          const functionName = (arg1, arg2, ... argN) => {
              return value;
          }

          例子

          const multiply = (a, b) => {
              return a * b;
          }
          console.log(multiply(7, 8)); // 56
          console.log(multiply(3, 2)); // 6

          關(guān)鍵特點(diǎn)

          • ? 箭頭函數(shù)類似匿名函數(shù)
          • ? 若只有一個(gè)參數(shù),可以省略小括號(hào)
             const square = x => {
                 return x * x;
             }
             console.log(square(2)); // 4
             console.log(square(7)); // 49

          這個(gè)情形的唯一陷阱是當(dāng)只有一個(gè)參數(shù)且需要解構(gòu)時(shí):

              const foo = ({name = "New User"}) => name;
              
              console.log(foo({})); // New User
              console.log(foo({name: "Parwinder"})); // Parwinder
          • ? 若沒有參數(shù),需要添加上小括號(hào)
             const greeting = () => {
                 return "Hello World!";
             }
             console.log(greeting()); // Hello World!
          • ? 若函數(shù)體是一個(gè)表達(dá)式且只返回該表達(dá)式,我們可以移出小括號(hào)和 return 關(guān)鍵字。
             const greeting = () => "Hello World!";
             console.log(greeting()); // Hello World

          現(xiàn)在我們知道了所有的關(guān)鍵特點(diǎn),讓我們來重寫獲取正方形的面積:

          const square = x => x * x;
          console.log(square(4)); // 16

          this 關(guān)鍵字

          this 關(guān)鍵字和函數(shù)

          JavaScript 中的 this 關(guān)鍵字是執(zhí)行上下文的一個(gè)屬性,它可能是全局的、函數(shù)內(nèi)的或者是 eval 中的。對(duì)于普通的函數(shù),this 會(huì)根據(jù)調(diào)用它方式不同而變化。

          1. 1. 函數(shù)直接調(diào)用時(shí),this 指向全局對(duì)象;
          2. 2. 用在構(gòu)造函數(shù)中,this 代表一個(gè)新對(duì)象;
          3. 3. 當(dāng)函數(shù)作為一個(gè)對(duì)象的方法被調(diào)用,this 就代表那個(gè)對(duì)象;
          4. 4. 在嚴(yán)格模式下, this 的值為 undefined;
          5. 5. 在事件中,this 指向接收到事件的元素;

          我們使用這種行為已經(jīng)很久了,以至于大多數(shù)JavaScript開發(fā)者都已經(jīng)習(xí)慣了。

          函數(shù)直接調(diào)用,this 代表全局對(duì)象

          function foo() {
              return this;
          };
          console.log(foo()); // window object in a browser, global object for node execution

          用在構(gòu)造函數(shù)中,this 代表一個(gè)新對(duì)象

          function Order(main, side, dessert) {
              this.main = main;
              this.side = side;
              this.dessert = dessert;
              this.order = function () {
                  return `I will have ${this.main} with ${this.side} and finish off with a ${this.dessert}`;
              }
          }
          const newOrder = new Order("sushi", "soup", "yogurt");
          console.log(newOrder.order());
          // I will have sushi with soup and finish off with a yogurt

          當(dāng)函數(shù)作為一個(gè)對(duì)象的方法被調(diào)用,this 就代表那個(gè)對(duì)象

          const myObject = {
              main: "butter chicken",
              side: "rice",
              dessert: "ice cream",
              order: function () {
                  return `I will have ${this.main} with ${this.side} and finish off with ${this.dessert}`;
              }
          }
          console.log(myObject.order());
          // I will have butter chicken with rice and finish off with ice cream

          上面的例子中,this 指向 myObject,可以獲取它上面的屬性。

          在嚴(yán)格模式下, this 的值為 undefined

          "use strict";
          function foo() {
              return this;
          };
          console.log(foo() === undefined); // true

          在事件中,this 指向接收到事件的元素


          主站蜘蛛池模板: 一区二区三区杨幂在线观看 | 三上悠亚日韩精品一区在线| 久久精品综合一区二区三区| 亚洲福利一区二区精品秒拍| 亚洲国产高清在线一区二区三区 | 亚洲永久无码3D动漫一区| 亚洲AV成人一区二区三区在线看| 亚洲日本一区二区三区在线不卡| 亚洲AV无码一区二区三区性色| 一本色道久久综合一区| 成人区人妻精品一区二区不卡| 国产AV一区二区三区传媒| 日本美女一区二区三区 | 在线精品亚洲一区二区三区| 在线精品一区二区三区电影| 91无码人妻精品一区二区三区L| 国产亚洲一区二区三区在线| 国产成人精品无人区一区 | 伊人久久精品无码麻豆一区| 久久久精品人妻一区二区三区 | 香蕉免费一区二区三区| 东京热无码一区二区三区av| 国产精品女同一区二区久久| 国产在线精品观看一区| 精品国产一区二区麻豆| 极品尤物一区二区三区| 亚洲一区二区三区在线| 乱码精品一区二区三区| 一区高清大胆人体| 国产成人精品一区二区三区免费| 国产成人精品无码一区二区三区 | 亚洲爆乳精品无码一区二区| 亚洲日韩精品国产一区二区三区| 亚洲天堂一区在线| 亚洲综合色一区二区三区小说| 亚洲一区在线观看视频| 精品无码AV一区二区三区不卡 | 一区二区三区美女视频| 亚洲中文字幕丝袜制服一区| 国产美女露脸口爆吞精一区二区| 精品视频在线观看你懂的一区|