面向?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ì)象。
在對(duì)象方法中, this 指向調(diào)用它所在方法的對(duì)象。
在上面一個(gè)實(shí)例中,this 表示 person 對(duì)象。
fullName 方法所屬的對(duì)象就是 person。
單獨(dú)使用 this,則它指向全局(Global)對(duì)象。
在瀏覽器中,window 就是該全局對(duì)象為 [object Window]:
如果單獨(dú)使用,this 也是指向全局(Global)對(duì)象。
在函數(shù)中,函數(shù)的所屬者默認(rèn)綁定到 this 上。
在瀏覽器中,window 就是該全局對(duì)象為 [object Window]:
嚴(yán)格模式下函數(shù)是沒有綁定到 this 上,這時(shí)候 this 是 undefined。
在 HTML 事件句柄中,this 指向了接收事件的 HTML 元素:
下面實(shí)例中,this 是 person 對(duì)象,person 對(duì)象是函數(shù)的所有者:
* this.firstName 表示 this (person) 對(duì)象的 firstName 屬性。
在 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方法。
原文: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
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
const greeting = () => {
return "Hello World!";
}
console.log(greeting()); // Hello World!
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
JavaScript 中的 this 關(guān)鍵字是執(zhí)行上下文的一個(gè)屬性,它可能是全局的、函數(shù)內(nèi)的或者是 eval 中的。對(duì)于普通的函數(shù),this 會(huì)根據(jù)調(diào)用它方式不同而變化。
我們使用這種行為已經(jīng)很久了,以至于大多數(shù)JavaScript開發(fā)者都已經(jīng)習(xí)慣了。
function foo() {
return this;
};
console.log(foo()); // window object in a browser, global object for node execution
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
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,可以獲取它上面的屬性。
"use strict";
function foo() {
return this;
};
console.log(foo() === undefined); // true
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。