Hướng dẫn explain this keyword in javascript - giải thích từ khóa này bằng javascript

Từ khóa ____22 của một hàm hoạt động khác một chút trong JavaScript so với các ngôn ngữ khác. Nó cũng có một số khác biệt giữa chế độ nghiêm ngặt và chế độ không nghiêm ngặt.function's

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.

Trong hầu hết các trường hợp, giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 được xác định bằng cách gọi một hàm (liên kết thời gian chạy). Nó không thể được đặt bằng cách gán trong quá trình thực hiện và nó có thể khác nhau mỗi khi hàm được gọi. Phương pháp
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
4 có thể đặt giá trị của
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của hàm bất kể nó được gọi là như thế nào và các hàm mũi tên không cung cấp ràng buộc
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của riêng chúng (nó giữ lại giá trị
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của bối cảnh từ vựng kèm theo).

Thử nó

Cú pháp

Giá trị

Một thuộc tính của bối cảnh thực thi (toàn cầu, chức năng hoặc eval), trong chế độ không đóng cửa, luôn luôn là một tham chiếu đến một đối tượng và trong chế độ nghiêm ngặt có thể là bất kỳ giá trị nào.

Sự mô tả

Bối cảnh toàn cầu

Trong bối cảnh thực thi toàn cầu (ngoài bất kỳ chức năng nào),

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 đề cập đến đối tượng toàn cầu cho dù ở chế độ nghiêm ngặt hay không.

// In web browsers, the window object is also the global object:
console.log(this === window); // true

a = 37;
console.log(window.a); // 37

this.b = "MDN";
console.log(window.b)  // "MDN"
console.log(b)         // "MDN"

Lưu ý: Bạn luôn có thể dễ dàng nhận được đối tượng toàn cầu bằng cách sử dụng thuộc tính toàn cầu ____29, bất kể bối cảnh hiện tại mà mã của bạn đang chạy. You can always easily get the global object using the global

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
9 property, regardless of the current context in which your code is running.

Chức năng bối cảnh

Bên trong một hàm, giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 phụ thuộc vào cách gọi hàm.

Vì mã sau không ở chế độ nghiêm ngặt và vì giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 không được đặt bởi cuộc gọi,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 sẽ mặc định cho đối tượng toàn cầu, đó là
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
3 trong trình duyệt.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true

Tuy nhiên, trong chế độ nghiêm ngặt, nếu giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 không được đặt khi nhập bối cảnh thực thi, thì nó vẫn là
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
5, như được hiển thị trong ví dụ sau:

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true

Lưu ý: Trong ví dụ thứ hai,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 phải là
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
5, vì
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
8 được gọi trực tiếp và không phải là phương thức hoặc thuộc tính của một đối tượng (ví dụ:
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
9). Tính năng này không được triển khai trong một số trình duyệt khi chúng lần đầu tiên bắt đầu hỗ trợ chế độ nghiêm ngặt. Kết quả là, họ đã trả lại không chính xác đối tượng
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
3.
In the second example,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 should be
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
5, because
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
8 was called directly and not as a method or property of an object (e.g.
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
9). This feature wasn't implemented in some browsers when they first started to support strict mode. As a result, they incorrectly returned the
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
3 object.

Để đặt giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 thành một giá trị cụ thể khi gọi hàm, sử dụng
class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
2 hoặc
class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
3 như trong các ví dụ dưới đây.

Bối cảnh lớp

Hành vi của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 trong các lớp và chức năng là tương tự nhau, vì các lớp là các chức năng dưới mui xe. Nhưng có một số khác biệt và cảnh báo.

Trong một hàm tạo lớp,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 là một đối tượng thông thường. Tất cả các phương pháp không tĩnh trong lớp được thêm vào nguyên mẫu của
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2:

class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']

Lưu ý: Phương pháp tĩnh không phải là thuộc tính của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2. Chúng là các thuộc tính của bản thân lớp. Static methods are not properties of
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2. They are properties of the class itself.

Các lớp học có nguồn gốc

Không giống như các hàm tạo lớp cơ sở, các hàm tạo dẫn xuất không có liên kết

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 ban đầu. Gọi
class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
9 tạo ra liên kết
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 trong hàm tạo và về cơ bản có tác dụng đánh giá dòng mã sau, trong đó cơ sở là lớp kế thừa:

Cảnh báo: Tham khảo

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 trước khi gọi
class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
9 sẽ gây ra lỗi.
Referring to
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 before calling
class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
9 will throw an error.

Các lớp dẫn xuất không được quay lại trước khi gọi

class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError
9, trừ khi họ trả lại
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
4 hoặc không có hàm tạo nào.

class Base {}
class Good extends Base {}
class AlsoGood extends Base {
  constructor() {
    return {a: 5};
  }
}
class Bad extends Base {
  constructor() {}
}

new Good();
new AlsoGood();
new Bad(); // ReferenceError

Ví dụ

Điều này trong bối cảnh chức năng

// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj

Điều này và chuyển đổi đối tượng

function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34

Lưu ý rằng trong chế độ không đóng cửa, với

// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
5 và
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
6, nếu giá trị được truyền dưới dạng
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 không phải là một đối tượng, một nỗ lực sẽ được thực hiện để chuyển đổi nó thành một đối tượng. Giá trị
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
8 và
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
5 trở thành đối tượng toàn cầu. Các nguyên thủy như
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
0 hoặc
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
1 sẽ được chuyển đổi thành một đối tượng bằng cách sử dụng hàm tạo liên quan, do đó số nguyên thủy
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
0 được chuyển đổi thành một đối tượng như thể bằng
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
3 và chuỗi
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
1 thành một đối tượng như thể bằng
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
5, ví dụ:

function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]

Phương thức BIND ()

Ecmascript 5 đã giới thiệu

function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
6. Gọi
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
7 tạo ra một chức năng mới với cùng một cơ thể và phạm vi như
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8, nhưng trong đó
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 xảy ra trong hàm gốc, trong hàm mới, nó liên kết vĩnh viễn với đối số đầu tiên của
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]
0, bất kể chức năng được sử dụng như thế nào.

function f() {
  return this.a;
}

const g = f.bind({ a: 'azerty' });
console.log(g()); // azerty

const h = g.bind({ a: 'yoo' }); // bind only works once!
console.log(h()); // azerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37,37, azerty, azerty

Hàm mũi tên

Trong các hàm mũi tên,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 giữ lại giá trị của bối cảnh từ vựng kèm theo
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2. Trong mã toàn cầu, nó sẽ được đặt thành đối tượng toàn cầu:

const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true

Lưu ý: Nếu

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 ARG được truyền đến
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
5,
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]
0 hoặc
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
6 khi gọi hàm mũi tên, nó sẽ bị bỏ qua. Bạn vẫn có thể dành cho các đối số cho cuộc gọi, nhưng đối số đầu tiên (
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]
7) phải được đặt thành
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
8.
If
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 arg is passed to
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
5,
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]
0, or
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
6 on invocation of an arrow function it will be ignored. You can still prepend arguments to the call, but the first argument (
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7);     // [object Number]
bar.call('foo'); // [object String]
bar.call(undefined); // [object global]
7) should be set to
// An object can be passed as the first argument to call
// or apply and this will be bound to it.
const obj = { a: 'Custom' };

// Variables declared with var become properties of the global object.
var a = 'Global';

function whatsThis() {
  return this.a;  // The value of this is dependent on how the function is called
}

whatsThis();          // 'Global' as this in the function isn't set, so it defaults to the global/window object in non–strict mode
whatsThis.call(obj);  // 'Custom' as this in the function is set to obj
whatsThis.apply(obj); // 'Custom' as this in the function is set to obj
8.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
0

Không có vấn đề gì, ____ 79

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 được đặt thành gì khi nó được tạo ra (trong ví dụ trên, đối tượng toàn cầu). Điều tương tự áp dụng cho các hàm mũi tên được tạo ra bên trong các chức năng khác:
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của chúng vẫn là của bối cảnh từ vựng kèm theo.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
1

Ở trên, hàm (gọi nó là hàm ẩn danh a) được gán cho

function f() {
  return this.a;
}

const g = f.bind({ a: 'azerty' });
console.log(g()); // azerty

const h = g.bind({ a: 'yoo' }); // bind only works once!
console.log(h()); // azerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37,37, azerty, azerty
2 trả về một hàm khác (gọi nó là hàm ẩn danh b) được tạo dưới dạng hàm mũi tên. Do đó,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của hàm B được đặt vĩnh viễn thành
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của
function f() {
  return this.a;
}

const g = f.bind({ a: 'azerty' });
console.log(g()); // azerty

const h = g.bind({ a: 'yoo' }); // bind only works once!
console.log(h()); // azerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37,37, azerty, azerty
2 (hàm A) khi được gọi. Khi hàm được trả về (hàm B) được gọi,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó sẽ luôn là những gì nó được đặt thành ban đầu. Trong ví dụ mã trên,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của hàm B được đặt thành hoạt động của A ____22 là
function f() {
  return this.a;
}

const g = f.bind({ a: 'azerty' });
console.log(g()); // azerty

const h = g.bind({ a: 'yoo' }); // bind only works once!
console.log(h()); // azerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37,37, azerty, azerty
9, do đó, nó vẫn được đặt thành
function f() {
  return this.a;
}

const g = f.bind({ a: 'azerty' });
console.log(g()); // azerty

const h = g.bind({ a: 'yoo' }); // bind only works once!
console.log(h()); // azerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37,37, azerty, azerty
9 ngay cả khi được gọi theo cách thường đặt
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 thành
class Example {
  constructor() {
    const proto = Object.getPrototypeOf(this);
    console.log(Object.getOwnPropertyNames(proto));
  }
  first(){}
  second(){}
  static third(){}
}

new Example(); // ['constructor', 'first', 'second']
5 hoặc đối tượng toàn cầu (hoặc bất kỳ phương thức nào khác như trong Ví dụ trước trong bối cảnh thực hiện toàn cầu).

Như một phương thức đối tượng

Khi một hàm được gọi là phương thức của một đối tượng,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó được đặt thành đối tượng, phương thức được gọi là bật.

Trong ví dụ sau, khi

const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
4 được gọi, bên trong hàm
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 được liên kết với đối tượng
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
6.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
2

Lưu ý rằng hành vi này hoàn toàn không bị ảnh hưởng bởi cách xác định hoặc ở đâu chức năng. Trong ví dụ trước, chúng tôi đã xác định chức năng nội tuyến là thành viên

function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8 trong định nghĩa của
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
6. Tuy nhiên, chúng ta có thể dễ dàng xác định chức năng trước và sau đó gắn nó vào
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
9. Làm như vậy dẫn đến cùng một hành vi:

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
3

Điều này chứng tỏ rằng chỉ có vấn đề là chức năng được gọi từ thành viên

function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8 của
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
6.

Tương tự, ràng buộc

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 chỉ bị ảnh hưởng bởi tham chiếu thành viên ngay lập tức nhất. Trong ví dụ sau, khi chúng tôi gọi hàm, chúng tôi gọi nó là phương thức
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
03 của đối tượng
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
04. Lần này trong quá trình thực thi,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 bên trong hàm sẽ đề cập đến
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
04. Thực tế là đối tượng tự nó là thành viên của
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
6 không có hậu quả; Tài liệu tham khảo ngay lập tức nhất là tất cả những gì quan trọng.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
4

Điều này trên chuỗi nguyên mẫu của đối tượng

Khái niệm tương tự cũng đúng với các phương thức được xác định ở đâu đó trên chuỗi nguyên mẫu của đối tượng. Nếu phương thức nằm trên chuỗi nguyên mẫu của một đối tượng,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 đề cập đến đối tượng, phương thức đã được gọi, như thể phương thức nằm trên đối tượng.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
5

Trong ví dụ này, đối tượng được gán cho biến

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
09 không có thuộc tính
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8 riêng của nó, nó kế thừa nó từ nguyên mẫu của nó. Nhưng không có vấn đề gì khi tìm kiếm
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8 cuối cùng tìm thấy một thành viên có tên đó trên
const globalObject = this;
const foo = (() => this);
console.log(foo() === globalObject); // true
6; Việc tra cứu bắt đầu như một tham chiếu đến
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
13, do đó
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 bên trong hàm lấy giá trị của đối tượng được gọi là
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
09. Đó là, vì
function add(c, d) {
  return this.a + this.b + c + d;
}

const o = { a: 1, b: 3 };

// The first parameter is the object to use as
// 'this', subsequent parameters are passed as
// arguments in the function call
add.call(o, 5, 7); // 16

// The first parameter is the object to use as
// 'this', the second is an array whose
// members are used as the arguments in the function call
add.apply(o, [10, 20]); // 34
8 được gọi là phương pháp của
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
09,
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó đề cập đến
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
09. Đây là một tính năng thú vị của kế thừa nguyên mẫu của JavaScript.

cái này với một getter hoặc setter

Một lần nữa, khái niệm tương tự cũng đúng khi một hàm được gọi từ một getter hoặc một setter. Một hàm được sử dụng như getter hoặc setter có

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 liên kết với đối tượng mà từ đó thuộc tính đang được đặt hoặc nhận.

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
6

Là một nhà xây dựng

Khi một hàm được sử dụng làm hàm tạo (với từ khóa

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
21),
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó bị ràng buộc với đối tượng mới được xây dựng.

Lưu ý: Mặc dù mặc định cho hàm tạo là trả về đối tượng được tham chiếu bởi

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2, thay vào đó, nó có thể trả về một số đối tượng khác (nếu giá trị trả về không phải là đối tượng, thì đối tượng
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 được trả về).
While the default for a constructor is to return the object referenced by
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2, it can instead return some other object (if the return value isn't an object, then the
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 object is returned).

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
7

Trong ví dụ cuối cùng (

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
25), bởi vì một đối tượng đã được trả lại trong quá trình xây dựng, đối tượng mới mà
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 bị ràng buộc sẽ bị loại bỏ. .

Là một người xử lý sự kiện DOM

Khi một hàm được sử dụng làm trình xử lý sự kiện,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó được đặt thành phần tử mà người nghe được đặt (một số trình duyệt không tuân theo quy ước này cho người nghe được thêm động với các phương thức khác với
function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
29).

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
8

Trong một người xử lý sự kiện nội tuyến

Khi mã được gọi từ một trình xử lý trực tuyến nội tuyến,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của nó sẽ được đặt thành phần tử DOM mà người nghe được đặt:

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
9

Cảnh báo trên cho thấy

function f1() {
  return this;
}

// In a browser:
f1() === window; // true

// In Node:
f1() === globalThis; // true
31. Tuy nhiên, lưu ý rằng chỉ có mã bên ngoài có
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 đặt theo cách này:

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
0

Trong trường hợp này,

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 của hàm bên trong không được đặt, vì vậy nó trả về đối tượng toàn cầu/cửa sổ (nghĩa là đối tượng mặc định trong chế độ không hạn chế trong đó
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 không được đặt bởi cuộc gọi).

Điều này trong các lớp học

Giống như với các chức năng thông thường, giá trị của

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 trong các phương thức phụ thuộc vào cách chúng được gọi. Đôi khi rất hữu ích khi ghi đè hành vi này để
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 trong các lớp luôn đề cập đến thể hiện lớp. Để đạt được điều này, hãy liên kết các phương thức lớp trong hàm tạo:

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
1

Lưu ý: Các lớp luôn là mã chế độ nghiêm ngặt.Các phương thức gọi với

function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 không xác định sẽ gây ra lỗi. Classes are always strict mode code. Calling methods with an undefined
function f2() {
  'use strict'; // see strict mode
  return this;
}

f2() === undefined; // true
2 will throw an error.

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-This-Keyword
# sec-this-keyword

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm