
- Javascript 基礎教程
- Javascript - 首頁
- JavaScript - 路線圖
- JavaScript - 概述
- JavaScript - 特性
- JavaScript - 啟用
- JavaScript - 位置
- JavaScript - 語法
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - 註釋
- JavaScript - 變數
- JavaScript - let 語句
- JavaScript - 常量
- JavaScript - 資料型別
- JavaScript - 型別轉換
- JavaScript - 嚴格模式
- JavaScript - 保留關鍵字
- JavaScript 運算子
- JavaScript - 運算子
- JavaScript - 算術運算子
- JavaScript - 比較運算子
- JavaScript - 邏輯運算子
- JavaScript - 位運算子
- JavaScript - 賦值運算子
- JavaScript - 條件運算子
- JavaScript - typeof 運算子
- JavaScript - 空值合併運算子
- JavaScript - delete 運算子
- JavaScript - 逗號運算子
- JavaScript - 分組運算子
- JavaScript - yield 運算子
- JavaScript - 展開運算子
- JavaScript - 指數運算子
- JavaScript - 運算子優先順序
- JavaScript 控制流
- JavaScript - if...else
- JavaScript - while 迴圈
- JavaScript - for 迴圈
- JavaScript - for...in
- Javascript - for...of
- JavaScript - 迴圈控制
- JavaScript - break 語句
- JavaScript - continue 語句
- JavaScript - switch case
- JavaScript - 使用者自定義迭代器
- JavaScript 函式
- JavaScript - 函式
- JavaScript - 函式表示式
- JavaScript - 函式引數
- JavaScript - 預設引數
- JavaScript - Function() 建構函式
- JavaScript - 函式提升
- JavaScript - 自執行函式
- JavaScript - 箭頭函式
- JavaScript - 函式呼叫
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - 閉包
- JavaScript - 變數作用域
- JavaScript - 全域性變數
- JavaScript - 智慧函式引數
- JavaScript 物件
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - String
- JavaScript - Array
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Set
- JavaScript - WeakSet
- JavaScript - Map
- JavaScript - WeakMap
- JavaScript - 可迭代物件
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - 模板字面量
- JavaScript - 標籤模板
- 面向物件的 JavaScript
- JavaScript - 物件
- JavaScript - 類
- JavaScript - 物件屬性
- JavaScript - 物件方法
- JavaScript - 靜態方法
- JavaScript - 顯示物件
- JavaScript - 物件訪問器
- JavaScript - 物件建構函式
- JavaScript - 原生原型
- JavaScript - ES5 物件方法
- JavaScript - 封裝
- JavaScript - 繼承
- JavaScript - 抽象
- JavaScript - 多型
- JavaScript - 解構賦值
- JavaScript - 物件解構
- JavaScript - 陣列解構
- JavaScript - 巢狀解構
- JavaScript - 可選鏈
- JavaScript - 全域性物件
- JavaScript - Mixin
- JavaScript - Proxy
- JavaScript 版本
- JavaScript - 歷史
- JavaScript - 版本
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript 非同步程式設計
- JavaScript - 非同步程式設計
- JavaScript - 回撥函式
- JavaScript - Promise
- JavaScript - Async/Await
- JavaScript - 微任務
- JavaScript - Promise 化
- JavaScript - Promise 鏈式呼叫
- JavaScript - 定時事件
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookie
- JavaScript - Cookie
- JavaScript - Cookie 屬性
- JavaScript - 刪除 Cookie
- JavaScript 瀏覽器 BOM
- JavaScript - 瀏覽器物件模型
- JavaScript - Window 物件
- JavaScript - Document 物件
- JavaScript - Screen 物件
- JavaScript - History 物件
- JavaScript - Navigator 物件
- JavaScript - Location 物件
- JavaScript - Console 物件
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript 事件
- JavaScript - 事件
- JavaScript - DOM 事件
- JavaScript - addEventListener()
- JavaScript - 滑鼠事件
- JavaScript - 鍵盤事件
- JavaScript - 表單事件
- JavaScript - Window/Document 事件
- JavaScript - 事件委託
- JavaScript - 事件冒泡
- JavaScript - 事件捕獲
- JavaScript - 自定義事件
- JavaScript 錯誤處理
- JavaScript - 錯誤處理
- JavaScript - try...catch
- JavaScript - 除錯
- JavaScript - 自定義錯誤
- JavaScript - 擴充套件錯誤
- JavaScript 重要關鍵字
- JavaScript - this關鍵字
- JavaScript - void 關鍵字
- JavaScript - new 關鍵字
- JavaScript - var 關鍵字
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM 方法和屬性
- JavaScript - DOM Document
- JavaScript - DOM 元素
- JavaScript - DOM 屬性 (Attr)
- JavaScript - DOM 表單
- JavaScript - 修改 HTML
- JavaScript - 修改 CSS
- JavaScript - DOM 動畫
- JavaScript - DOM 導航
- JavaScript - DOM 集合
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript 其他
- JavaScript - Ajax
- JavaScript - 非同步迭代
- JavaScript - Atomics 物件
- JavaScript - rest 引數
- JavaScript - 頁面重定向
- JavaScript - 對話方塊
- JavaScript - 頁面列印
- JavaScript - 驗證
- JavaScript - 動畫
- JavaScript - 多媒體
- JavaScript - 圖片地圖
- JavaScript - 瀏覽器
- JavaScript - JSON
- JavaScript - 多行字串
- JavaScript - 日期格式
- JavaScript - 獲取日期的方法
- JavaScript - 設定日期的方法
- JavaScript - 模組
- JavaScript - 動態匯入
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - 淺複製
- JavaScript - 呼叫棧
- JavaScript - 引用型別
- JavaScript - IndexedDB
- JavaScript - 點選劫持攻擊
- JavaScript - 柯里化
- JavaScript - 圖形
- JavaScript - Canvas
- JavaScript - 防抖
- JavaScript - 效能
- JavaScript - 風格指南
JavaScript - this關鍵字
什麼是 'this' 關鍵字?
在 JavaScript 中,'this' 關鍵字包含對物件的引用。它表示函式或當前程式碼的上下文。它用於訪問當前物件的屬性和方法。
當在函式內部使用 this 關鍵字時,this 將引用呼叫該函式的物件。
在 JavaScript 中,函式也是物件。因此,您也可以將 'this' 關鍵字與函式一起使用。
'this' 指的是哪個物件?
'this' 關鍵字引用的物件取決於您如何使用 'this' 關鍵字。
例如:
'this' 關鍵字在全域性作用域中引用 window 物件。
當您在函式內部使用 'this' 關鍵字時,它也表示 'window' 物件。
在函式的嚴格模式下,'this' 關鍵字引用 undefined。
當您在物件方法中使用它時,'this' 關鍵字引用該物件。
在事件處理程式中,'this' 關鍵字引用執行該事件的元素。
在 call()、apply() 和 bind() 等方法中的 'this' 關鍵字可以引用不同的物件。
語法
遵循以下語法在 JavaScript 中使用 'this' 關鍵字:
this.property OR this.method();
您可以使用 'this' 關鍵字訪問屬性並執行物件的方法。
JavaScript 中全域性作用域的 'this'
當您在全域性作用域中使用 'this' 關鍵字時,它表示全域性 (window) 物件。您可以使用全域性作用域中的 'this' 關鍵字訪問全域性變數。
示例
在下面的程式碼中,我們在全域性作用域中定義了 'num' 變數和 printNum() 函式。之後,我們使用 'this' 關鍵字訪問全域性變數和函式。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); var num = 10; function printNum() { output.innerHTML += "Inside the function: " + num + "<br>"; } this.printNum(); output.innerHTML += "Outside the function: " + this.num + "<br>"; </script> </body> </html>
輸出
Inside the function: 10 Outside the function: 10
JavaScript 函式中的 'this'
當您在函式中使用 'this' 關鍵字時,它表示全域性作用域或 'window' 物件。
示例
在下面的程式碼中,我們在函式內部使用了 'this' 關鍵字。您可以觀察到我們使用函式內部的 'this' 關鍵字訪問全域性變數。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); var message = "Hello World!"; function printMessage() { var message = "Hi! How are you?"; output.innerHTML = "The messsage is: " + this.message; } printMessage(); </script> </body> </html>
輸出
The messsage is: Hello World!
嚴格模式下函式中的 'this'
當您在嚴格模式下的函式內部使用 'this' 關鍵字時,它不引用任何物件。'this' 關鍵字的值變為 undefined。
示例
在下面的程式碼中,我們在嚴格模式下的函式內部使用了 'this' 關鍵字。它列印 undefined。
<html> <body> <div id = "demo"> </div> <script> let output = document.getElementById('demo'); var message = "Hello World!"; function test() { "use strict"; output.innerHTML = "The value of this in the strict mode is: " + this; } test(); </script> </body> </html>
輸出
The value of this in the strict mode is: undefined
建構函式中的 'this'
當您將函式用作建構函式來建立物件時,'this' 關鍵字引用該物件。
示例
我們在下面的程式碼中定義了 Animal() 建構函式。我們在建構函式內部使用了 'this' 關鍵字來初始化物件的屬性。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); function Animal() { this.name = 'Lion'; this.age = 3; this.color = 'Brown'; } const newAnimal = new Animal(); output.innerHTML = "Animal Name: " + newAnimal.name + "<br>"; output.innerHTML += "Animal Age: " + newAnimal.age + "<br>"; output.innerHTML += "Animal Color: " + newAnimal.color; </script> </body> </html>
輸出
Animal Name: Lion Animal Age: 3 Animal Color: Brown
箭頭函式中的 'this'
當您在箭頭函式中使用 'this' 關鍵字時,它引用其父物件的範圍。
例如,當您在物件方法內部定義箭頭函式並在其中使用 'this' 關鍵字時,它表示該物件。如果您在另一個函式內部定義箭頭函式,則 'this' 關鍵字引用全域性物件。
示例
在下面的程式碼中,我們在物件的 getDetails() 方法內部定義了箭頭函式。當我們列印 'this' 關鍵字的值時,它列印該物件。
<html> <body> <div id = "output1">Value of 'this' inside the getDetails() method: </div> <div id = "output2">Value of 'this' inside the getInnerDetails() method: </div> <script> const wall = { size: "10", color: "blue", getDetails() { document.getElementById('output1').innerHTML += JSON.stringify(this); const getInnerDetails = () => { document.getElementById('output2').innerHTML += JSON.stringify(this); } getInnerDetails(); } } wall.getDetails(); </script> </body> </html>
輸出
Value of 'this' inside the getDetails() method: {"size":"10","color":"blue"} Value of 'this' inside the getInnerDetails() method: {"size":"10","color":"blue"}
物件方法中的 'this'
當您在物件方法內部使用 'this' 關鍵字時,它表示物件本身。
示例
在下面的程式碼中,我們定義了 'fruit' 物件。該物件包含 printFruitDetails() 方法,在該方法中,我們使用了 'this' 關鍵字來訪問物件的屬性。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); const fruit = { name: "Apple", color: "red", printFruitDetails() { output.innerHTML += "Furit Name = " + this.name + "<br>"; output.innerHTML += "Fruit Color = " + this.color; } } fruit.printFruitDetails(); </script> </body> </html>
輸出
Furit Name = Apple Fruit Color = red
物件方法的子函式中的 'this'
當您在物件方法內部定義函式並在該函式內部使用 'this' 關鍵字時,它表示全域性物件而不是物件。
示例
在下方的程式碼中,我們定義了 person 物件。person 物件包含 printDetails() 方法。在 printDetails() 方法中,我們定義了 printThis() 函式。
在 printThis() 函式中,我們列印了“this”關鍵字的值,它列印的是全域性物件。
<html> <body> <div id = "output">Inside the printThis() function, Value of 'this' = </div> <script> const person = { name: "Salman", isBusinessman: false, printDetails() { function printThis() { document.getElementById('output').innerHTML += this; } printThis(); } } person.printDetails(); </script> </body> </html>
輸出
Inside the printThis() function, Value of 'this' = [object Window]
JavaScript 中事件處理程式中的“this”
在事件處理程式中使用“this”關鍵字指的是執行事件的 HTML 元素。
示例
在下方的程式碼中,我們在 <div> 元素中添加了 onClick 事件處理程式。當用戶點選 div 元素時,我們使用“display”屬性隱藏 div 元素。
<html> <head> <style> div { height: 200px; width: 700px; background-color: red; } </style> </head> <body> <p>Click the DIV below to remove it. </p> <div onclick = "this.style.display = 'none'"> </div> </body> </html>
JavaScript 中的顯式函式繫結
在 JavaScript 中,call()、apply() 或 bind() 方法用於顯式繫結。
顯式繫結允許你借用特定物件的的方法。使用這些方法,你可以顯式地定義“this”關鍵字的上下文。
讓我們透過下面的例子來理解顯式繫結。
示例:使用 call() 方法
在下方的程式碼中,lion 物件包含 color 和 age 屬性。它還包含 printDetails() 方法,並使用“this”關鍵字列印詳細資訊。
tiger 物件只包含 color 和 age 屬性。我們使用 call() 方法來呼叫 lion 物件的 printDetails() 方法,並使用 tiger 物件作為上下文。因此,該方法會輸出 tiger 的詳細資訊。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); const lion = { color: "Yellow", age: 10, printDetails() { output.innerHTML += `<p>Color: ${this.color}</p>`; output.innerHTML += `<p>Age: ${this.age}</p>`; } } const tiger = { color: "Orange", age: 15, } lion.printDetails.call(tiger); </script> </body> </html>
輸出
Color: Orange Age: 15
示例:使用 bind() 方法
下面的程式碼也包含 lion 和 tiger 物件。之後,我們使用 bind() 方法將 lion 物件的 printDetails() 方法繫結到 tiger 物件。
之後,我們使用 tigerDetails() 方法列印 tiger 物件的詳細資訊。
<html> <body> <div id = "demo"> </div> <script> const output = document.getElementById('demo'); const lion = { color: "Yellow", age: 10, printDetails() { output.innerHTML += `<p>Color: ${this.color}</p>`; output.innerHTML += `<p>Age: ${this.age}</p>`; } } const tiger = { color: "Orange", age: 15, } const tigerDetails = lion.printDetails.bind(tiger); tigerDetails(); </script> </body> </html>
輸出
Color: Orange Age: 15
JavaScript “this” 的優先順序
你應該使用以下優先順序順序來確定“this”關鍵字的上下文。
- 1. bind() 方法
- 2. call 和 apply() 方法
- 3. 物件方法
- 4. 全域性作用域