Từ khóa này trong JavaScript là gì

Khi

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 được sử dụng một mình,
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng toàn cầu [đối tượng
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
7 trong trình duyệt]. Ví dụ,

let a = this;
console.log[a];  // Window {}

this.name = 'Sarah';
console.log[window.name]; // Sarah

Ở đây,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
8 giống như
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
9

2. chức năng bên trong này

Khi

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 được sử dụng trong một chức năng,
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng toàn cầu [đối tượng
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
7 trong trình duyệt]. Ví dụ,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}

3. chức năng xây dựng bên trong này

Trong JavaScript, hàm tạo được sử dụng để tạo đối tượng. Khi một hàm được sử dụng làm hàm tạo,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng bên trong mà nó được sử dụng. Ví dụ,

function Person[] {

    this.name = 'Jack';
    console.log[this];

}

let person1 = new Person[];
console.log[person1.name];

đầu ra

Person {name: "Jack"}
Jack

Ở đây,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng person1. Đó là lý do tại sao,
function Person[] {

    this.name = 'Jack';
    console.log[this];

}

let person1 = new Person[];
console.log[person1.name];
5 mang đến cho chúng ta Jack

Ghi chú. Khi

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 được sử dụng với các lớp ES6, nó đề cập đến đối tượng bên trong mà nó được sử dụng [tương tự như hàm tạo]

4. Phương thức đối tượng bên trong này

Khi

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 được sử dụng bên trong một phương thức của đối tượng, thì
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng mà nó nằm bên trong. Ví dụ,

const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];
        console.log[this.name];
    }
}

person.greet[];

đầu ra

{name: "Jack", age: 25, greet: ƒ}
Jack

Trong ví dụ trên,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng
Person {name: "Jack"}
Jack
0

5. chức năng bên trong bên trong này

Khi bạn truy cập

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 bên trong một hàm bên trong [bên trong một phương thức],
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng toàn cầu. Ví dụ,

const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];        // {name: "Jack", age ...}
        console.log[this.age];  // 25

        // inner function
        function innerFunc[] {
        
            // this refers to the global object
            console.log[this];       // Window { .. }
            console.log[this.age];    // undefined
            
        }

        innerFunc[];

    }
}

person.greet[];

đầu ra

{name: "Jack", age: 25, greet: ƒ}
25
Window { …}
undefined

Ở đây,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 bên trong
Person {name: "Jack"}
Jack
4 đề cập đến đối tượng toàn cầu vì
Person {name: "Jack"}
Jack
4 nằm trong một phương thức

Tuy nhiên,

Person {name: "Jack"}
Jack
6 bên ngoài
Person {name: "Jack"}
Jack
4 đề cập đến đối tượng
Person {name: "Jack"}
Jack
0

6. chức năng mũi tên bên trong này

Bên trong hàm mũi tên,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến phạm vi cha. Ví dụ,

const greet = [] => {
    console.log[this];
}
greet[]; // Window {...}

Hàm mũi tên không có

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 riêng. Khi bạn sử dụng
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 bên trong hàm mũi tên,
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 đề cập đến đối tượng phạm vi cha của nó. Ví dụ,

const greet = {
    name: 'Jack',

    // method
    sayHi [] {
        let hi = [] => console.log[this.name];
        hi[];
    }
}

greet.sayHi[]; // Jack

Ở đây,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
8 bên trong hàm
const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];
        console.log[this.name];
    }
}

person.greet[];
4 đề cập đến đối tượng
const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];
        console.log[this.name];
    }
}

person.greet[];
5

Bạn cũng có thể sử dụng hàm mũi tên để giải quyết vấn đề có

const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];
        console.log[this.name];
    }
}

person.greet[];
6 khi sử dụng một hàm bên trong một phương thức [như đã thấy trong Ví dụ 5]. Ví dụ,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
0

đầu ra

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
1

Ở đây,

Person {name: "Jack"}
Jack
4 được xác định bằng chức năng mũi tên. Nó lấy
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 từ phạm vi cha của nó. Do đó,
Person {name: "Jack"}
Jack
6 cho 25

Khi chức năng mũi tên được sử dụng với

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4, nó đề cập đến phạm vi bên ngoài

7. Chức năng bên trong này với Chế độ nghiêm ngặt

Khi

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 được sử dụng trong một hàm có chế độ nghiêm ngặt, thì
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 là
const person = {
    name : 'Jack',
    age: 25,

    // this inside method
    // this refers to the object itself
    greet[] {
        console.log[this];
        console.log[this.name];
    }
}

person.greet[];
6. Ví dụ,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
2

Ghi chú. Khi sử dụng

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 bên trong một hàm có chế độ nghiêm ngặt, bạn có thể sử dụng lệnh gọi hàm JavaScript[]

Ví dụ,

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
3

Khi bạn vượt qua

function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 với hàm
{name: "Jack", age: 25, greet: ƒ}
Jack
6, thì
{name: "Jack", age: 25, greet: ƒ}
Jack
7 được coi là phương thức của đối tượng
function greet[] {

    // this inside function
    // this refers to the global object
    console.log[this];
}

greet[]; // Window {}
4 [đối tượng toàn cục trong trường hợp này]

Chức năng của từ khóa này là gì?

Từ khóa this đề cập đến đối tượng hiện tại trong một phương thức hoặc hàm tạo. Cách sử dụng phổ biến nhất của từ khóa this là để loại bỏ sự nhầm lẫn giữa thuộc tính lớp và tham số có cùng tên [vì thuộc tính lớp bị ẩn bởi phương thức .

Sáu cách để sử dụng từ khóa này là gì?

Lập trình C++ .
cái này có thể được sử dụng để lấy đối tượng hiện tại
điều này có thể được sử dụng để gọi phương thức của đối tượng hiện tại
this[] có thể được sử dụng để gọi hàm tạo của lớp hiện tại
cái này có thể được truyền dưới dạng tham số cho lệnh gọi phương thức
cái này có thể được truyền dưới dạng tham số cho hàm tạo

Việc sử dụng từ khóa này trong jQuery là gì?

từ khóa này. Trong JavaScript, từ khóa này được sử dụng để chỉ đối tượng mà nó thuộc về . Giá trị này lưu trữ là bối cảnh thực thi hiện tại của chương trình JavaScript.

Bốn nguyên tắc của từ khóa này trong JavaScript là gì?

Từ khóa this, khi được sử dụng trong một hàm, sẽ liên kết hàm đó với một đối tượng ngữ cảnh. Có bốn loại ràng buộc. liên kết mặc định, liên kết ngầm, liên kết rõ ràng và liên kết lệnh gọi hàm tạo [mới] Biết bốn quy tắc này sẽ giúp bạn dễ dàng phân biệt ngữ cảnh của tham chiếu này.

Chủ Đề