You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
The`instanceof`operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.
Toán tử`instanceof`cho phép kiểm tra xem một đối tượng có thuộc một class nào đó hay không. Nó cũng tính đến sự kế thừa.
Such a check may be necessary in many cases. For example, it can be used for building a *polymorphic* function, the one that treats arguments differently depending on their type.
Việc kiểm tra như vậy có thể cần thiết trong nhiều trường hợp. Ví dụ: nó có thể được sử dụng để xây dựng hàm *đa hình*, hàm xử lý các đối số khác nhau tùy thuộc vào loại của chúng.
##The instanceof operator [#ref-instanceof]
##Toán tử instanceof [#ref-instanceof]
The syntax is:
Cú pháp là:
```js
obj instanceof Class
```
It returns`true`if `obj`belongs to the`Class`or a classinheriting from it.
Nó trả về`true`nếu `obj`thuộc về`Class`hoặc một classkế thừa từ nó.
For instance:
Ví dụ:
```js run
class Rabbit {}
let rabbit = new Rabbit();
//is it an object of Rabbitclass?
//nó có phải là đối tượng củaclass Rabbit không?
*!*
alert( rabbit instanceof Rabbit ); // true
*/!*
```
It also works with constructor functions:
Nó cũng hoạt động với các hàm tạo:
```js run
*!*
//instead of class
//thay vì class
function Rabbit() {}
*/!*
alert( new Rabbit() instanceof Rabbit ); // true
```
...And with built-in classes like `Array`:
...Và với các class tích hợp như `Array`:
```js run
let arr = [1, 2, 3];
alert( arr instanceof Array ); // true
alert( arr instanceof Object ); // true
```
Please note that`arr`also belongs to the`Object` class. That's because`Array`prototypically inherits from `Object`.
Hãy lưu ý rằng`arr`cũng thuộc class`Object`. Đó là bởi vì`Array`kế thừa nguyên mẫu từ `Object`.
Normally, `instanceof`examines the prototype chain for the check. We can also set a custom logicin the static method `Symbol.hasInstance`.
Thông thường, `instanceof`kiểm tra chuỗi nguyên mẫu để kiểm tra. Chúng ta cũng có thể đặt logictùy chỉnh trong phương thức tĩnh `Symbol.hasInstance`.
The algorithm of `obj instanceof Class`works roughly as follows:
Thuật toán của `obj instanceof Class`hoạt động đại khái như sau:
1.If there's a static method`Symbol.hasInstance`,then just call it: `Class[Symbol.hasInstance](obj)`.It should return either `true`or `false`,and we're done. That's how we can customize the behavior of `instanceof`.
1.Nếu có một phương thức tĩnh`Symbol.hasInstance`,thì chỉ cần gọi nó là: `Class[Symbol.hasInstance](obj)`.Nó sẽ trả về `true`hoặc `false`,và chúng ta đã hoàn tất. Đó là cách chúng ta có thể tùy chỉnh hành vi của `instanceof`.
For example:
Ví dụ:
```js run
//setupinstanceOfcheck that assumes that
//anything withcanEatproperty is an animal
//thiết lậpinstanceOfkiểm tra giả định rằng
//bất cứ thứ gì có thuộc tínhcanEatđều là động vật
class Animal {
static [Symbol.hasInstance](obj) {
if (obj.canEat) return true;
Expand All
@@ -65,24 +65,24 @@ The algorithm of `obj instanceof Class` works roughly as follows:
let obj = { canEat: true };
alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj)is called
alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj)được gọi
```
2.Most classes do not have`Symbol.hasInstance`.In that case, the standard logic is used: `obj instanceOf Class`checks whether`Class.prototype`is equal to one of the prototypes in the`obj`prototype chain.
2.Hầu hết các class không có`Symbol.hasInstance`.Trong trường hợp đó, logic tiêu chuẩn được sử dụng: `obj instanceOf Class`kiểm tra xem`Class.prototype`có bằng với một trong các nguyên mẫu trong chuỗi nguyên mẫu`obj`hay không.
Here's the illustration of what`rabbit instanceof Animal`compares with `Animal.prototype`:
Đây là hình minh họa về những gì`rabbit instanceof Animal`so sánh với `Animal.prototype`:

By the way, there's also a method[objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf),that returns `true`if `objA`is somewhere in the chain of prototypes for`objB`.So the test of`obj instanceof Class`can be rephrased as `Class.prototype.isPrototypeOf(obj)`.
Nhân tiện, cũng có một phương thức[objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf),trả về `true`nếu `objA`nằm ở đâu đó trong chuỗi nguyên mẫu cho`objB`.Vì vậy, bài kiểm tra của`obj instanceof Class`có thể được diễn đạt lại thành `Class.prototype.isPrototypeOf(obj)`.
It's funny, but the`Class`constructor itself does not participate in the check! Only the chain of prototypes and`Class.prototype`matters.
Thật buồn cười, nhưng bản thân hàm tạo`Class`không tham gia kiểm tra! Chỉ chuỗi nguyên mẫu và`Class.prototype`mới quan trọng.
That can lead to interesting consequences when a`prototype`property is changed after the object is created.
Điều đó có thể dẫn đến những hậu quả thú vị khi thuộc tính`prototype`bị thay đổi sau khi đối tượng được tạo.
Like here:
Như ở đây:
```js run
function Rabbit() {}
let rabbit = new Rabbit();
//changed the prototype
//đã thay đổi nguyên mẫu
Rabbit.prototype = {};
// ...not a rabbit any more!
// ...không còn là thỏ nữa!
*!*
alert( rabbit instanceof Rabbit ); // false
*/!*
```
## Bonus: Object.prototype.toStringfor the type
## Bonus: Object.prototype.toStringcho loại
We already know that plain objects are converted to string as `[object Object]`:
Chúng ta đã biết rằng các đối tượng đơn giản được chuyển đổi thành chuỗi dưới dạng `[object Object]`:
```js run
let obj = {};
alert(obj); // [object Object]
alert(obj.toString()); //the same
alert(obj.toString()); //giống nhau
```
That's their implementation of`toString`. But there's a hidden feature that makes`toString`actually much more powerful than that. We can use it as an extended`typeof`and an alternative for `instanceof`.
Đó là cách triển khai`toString` của chúng. Nhưng có một tính năng ẩn làm cho`toString`thực sự mạnh hơn thế nhiều. Chúng ta có thể sử dụng nó như một`typeof`mở rộng và thay thế cho `instanceof`.
Sounds strange? Indeed. Let's demystify.
Thấy lạ không? Thực vậy. Hãy làm sáng tỏ
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring),the built-in`toString`can be extracted from the object and executed in the context of any other value. And its result depends on that value.
Theo [thông số kỹ thuật](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), `toString`tích hợp sẵn có thể được trích xuất từ đối tượng và được thực thi trong ngữ cảnh của bất kỳ giá trị nào khác. Và kết quả của nó phụ thuộc vào giá trị đó.
-For a number, it will be `[object Number]`
-For aboolean,it will be `[object Boolean]`
-For `null`: `[object Null]`
-For `undefined`: `[object Undefined]`
-For arrays: `[object Array]`
- ...etc (customizable).
-Đối với một số, nó sẽ là `[object Number]`
-Đối với mộtboolean,nó sẽ là `[object Boolean]`
-Đối với `null`: `[object Null]`
-Đối với `undefined`: `[object Undefined]`
-Đối với các array: `[object Array]`
- ...v.v (có thể tùy chỉnh).
Let's demonstrate:
Hãy chứng minh:
```js run
//copytoStringmethod into a variable for convenience
//sao chép phương thứctoStringvào một biến để thuận tiện
Here we used[call](mdn:js/function/call)as described in the chapter[](info:call-apply-decorators)to execute the function `objectToString`in the context `this=arr`.
Ở đây, chúng ta đã sử dụng[call](mdn:js/function/call)như được mô tả trong chương[](info:call-apply-decorators)để thực thi hàm `objectToString`trong ngữ cảnh `this=arr`.
Internally, the`toString`algorithm examines `this`and returns the corresponding result. More examples:
Bên trong, thuật toán`toString`kiểm tra `this`và trả về kết quả tương ứng. Thêm ví dụ:
As you can see, the result is exactly`Symbol.toStringTag` (if exists),wrapped into`[object...]`.
Như bạn có thể thấy, kết quả chính xác là`Symbol.toStringTag` (nếu tồn tại),được bao bọc trong`[object...]`.
At the end we have "typeof on steroids"that not only works for primitive data types, but also for built-in objects and even can be customized.
Cuối cùng, chúng ta có "typeof on steroids"không chỉ hoạt động với các kiểu dữ liệu nguyên thủy mà còn cho các đối tượng tích hợp sẵn và thậm chí có thể được tùy chỉnh.
We can use`{}.toString.call`instead of `instanceof`for built-in objects when we want to get the type as a string rather than just to check.
Chúng ta có thể sử dụng`{}.toString.call`thay vì `instanceof`cho các đối tượng tích hợp khi chúng ta muốn lấy loại dưới dạng chuỗi thay vì chỉ để kiểm tra.
##Summary
##Tóm tắt
Let's summarize the type-checking methods that we know:
Hãy tóm tắt các phương pháp kiểm tra loại mà chúng ta biết:
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.