Toán tử
5 kiểm tra xem thuộc tính // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
6 của hàm tạo xuất hiện ở bất cứ đâu trong chuỗi nguyên mẫu của một đối tượng. Giá trị trả về là một giá trị boolean. Hành vi của nó có thể được tùy chỉnh với // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
7.// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 operator tests to see if the // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
6 property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. Its behavior can be customized with // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
7.// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
Thử nó
Cú pháp
object instanceof constructor
Thông số
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
8Đối tượng để kiểm tra.
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9Người xây dựng để kiểm tra chống lại.
Ngoại lệ
class A {}
class B extends A {}
const o1 = new A[];
// true, because Object.getPrototypeOf[o1] === A.prototype
o1 instanceof A;
// false, because B.prototype is nowhere in o1's prototype chain
o1 instanceof B;
const o2 = new B[];
// true, because Object.getPrototypeOf[Object.getPrototypeOf[o2]] === A.prototype
o2 instanceof A;
// true, because Object.getPrototypeOf[o2] === B.prototype
o2 instanceof B;
0Ném nếu
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9 không phải là một đối tượng. Nếu // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9 không có phương thức class A {}
class B extends A {}
const o1 = new A[];
// true, because Object.getPrototypeOf[o1] === A.prototype
o1 instanceof A;
// false, because B.prototype is nowhere in o1's prototype chain
o1 instanceof B;
const o2 = new B[];
// true, because Object.getPrototypeOf[Object.getPrototypeOf[o2]] === A.prototype
o2 instanceof A;
// true, because Object.getPrototypeOf[o2] === B.prototype
o2 instanceof B;
3, nó cũng phải là một hàm.Sự mô tả
Toán tử
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 kiểm tra sự hiện diện của class A {}
class B extends A {}
const o1 = new A[];
// true, because Object.getPrototypeOf[o1] === A.prototype
o1 instanceof A;
// false, because B.prototype is nowhere in o1's prototype chain
o1 instanceof B;
const o2 = new B[];
// true, because Object.getPrototypeOf[Object.getPrototypeOf[o2]] === A.prototype
o2 instanceof A;
// true, because Object.getPrototypeOf[o2] === B.prototype
o2 instanceof B;
5 trong chuỗi nguyên mẫu của ____ 18. Điều này thường [mặc dù không phải lúc nào] có nghĩa là // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
8 được xây dựng với // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9.// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
Lưu ý rằng giá trị của thử nghiệm
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 có thể thay đổi nếu class A {}
class B extends A {}
const o1 = new A[];
// true, because Object.getPrototypeOf[o1] === A.prototype
o1 instanceof A;
// false, because B.prototype is nowhere in o1's prototype chain
o1 instanceof B;
const o2 = new B[];
// true, because Object.getPrototypeOf[Object.getPrototypeOf[o2]] === A.prototype
o2 instanceof A;
// true, because Object.getPrototypeOf[o2] === B.prototype
o2 instanceof B;
5 được gán lại sau khi tạo đối tượng [thường không được khuyến khích]. Nó cũng có thể được thay đổi bằng cách thay đổi nguyên mẫu của ____ 18 bằng cách sử dụng class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
2.Các lớp hoạt động theo cùng một cách, bởi vì các lớp cũng có thuộc tính
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
6.class A {}
class B extends A {}
const o1 = new A[];
// true, because Object.getPrototypeOf[o1] === A.prototype
o1 instanceof A;
// false, because B.prototype is nowhere in o1's prototype chain
o1 instanceof B;
const o2 = new B[];
// true, because Object.getPrototypeOf[Object.getPrototypeOf[o2]] === A.prototype
o2 instanceof A;
// true, because Object.getPrototypeOf[o2] === B.prototype
o2 instanceof B;
Đối với các chức năng ràng buộc,
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 tìm kiếm thuộc tính // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
6 trên hàm đích, vì các hàm ràng buộc không có // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
6.class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
Instanceof và @@ Hasinstance
Nếu
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9 có phương thức // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
7, phương thức sẽ được gọi là ưu tiên, với // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
8 là đối số duy nhất của nó và // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
9 là // This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
1.// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
ví dụ và nhiều cõi
Môi trường thực thi JavaScript [Windows, khung, v.v.] đều nằm trong vương quốc của riêng họ. Điều này có nghĩa là chúng có các tích hợp khác nhau [đối tượng toàn cầu khác nhau, các hàm tạo khác nhau, v.v.]. Điều này có thể dẫn đến kết quả bất ngờ. Chẳng hạn,
// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
2 sẽ trả lại // This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
3, bởi vì // This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
4 và các mảng trong vương quốc hiện tại kế thừa từ cái trước.Điều này có thể không có ý nghĩa lúc đầu, nhưng đối với các tập lệnh liên quan đến nhiều khung hoặc cửa sổ và chuyển các đối tượng từ ngữ cảnh này sang ngữ cảnh khác thông qua các chức năng, đây sẽ là một vấn đề hợp lệ và mạnh mẽ. Chẳng hạn, bạn có thể kiểm tra một cách an toàn xem một đối tượng đã cho trên thực tế là một mảng sử dụng
// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
5, bỏ qua phạm vi nào nó đến từ.Ví dụ: để kiểm tra xem
// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
6 có phải là // This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
7 trong bối cảnh khác không, bạn có thể sử dụng // This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
8.Ví dụ
Sử dụng InstanceOf với Chuỗi
Ví dụ sau đây cho thấy hành vi của
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 với các đối tượng const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
0.const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
Sử dụng Instanceof với ngày
Ví dụ sau đây cho thấy hành vi của
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 với các đối tượng const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
2.const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
Các đối tượng được tạo bằng Object.Create []
Ví dụ sau đây cho thấy hành vi của
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 với các đối tượng được tạo bằng const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
4.function Shape[] {}
function Rectangle[] {
Shape.call[this]; // call super constructor.
}
Rectangle.prototype = Object.create[Shape.prototype];
Rectangle.prototype.constructor = Rectangle;
const rect = new Rectangle[];
rect instanceof Object; // true
rect instanceof Shape; // true
rect instanceof Rectangle; // true
rect instanceof String; // false
const literalObject = {};
const nullObject = Object.create[null];
nullObject.name = "My object";
literalObject instanceof Object; // true, every object literal has Object.prototype as prototype
[{} instanceof Object]; // true, same case as above
nullObject instanceof Object; // false, prototype is end of prototype chain [null]
Chứng minh rằng mycar thuộc loại xe và loại đối tượng
Mã sau tạo một loại đối tượng
const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
5 và một thể hiện của loại đối tượng đó, const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
6. Toán tử // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 chứng minh rằng đối tượng const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
6 thuộc loại const literalString = "This is a literal string";
const stringObject = new String["String created with constructor"];
literalString instanceof String; // false, string primitive is not a String
stringObject instanceof String; // true
literalString instanceof Object; // false, string primitive is not an Object
stringObject instanceof Object; // true
stringObject instanceof Date; // false
5 và loại const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
0.function Car[make, model, year] {
this.make = make;
this.model = model;
this.year = year;
}
const mycar = new Car["Honda", "Accord", 1998];
const a = mycar instanceof Car; // returns true
const b = mycar instanceof Object; // returns true
Không phải là một ví dụ
Để kiểm tra xem một đối tượng không phải là một hàm tạo cụ thể, bạn có thể làm:
if [![mycar instanceof Car]] {
// Do something, like:
// mycar = new Car[mycar]
}
Điều này thực sự khác với:
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
0Điều này sẽ luôn luôn là
// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
3. .Ghi đè hành vi của thể hiện
Một cạm bẫy phổ biến của việc sử dụng
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 là tin rằng, nếu const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
7, thì const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
8 đã được tạo bằng cách sử dụng const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
9 làm hàm tạo. Điều này không đúng, bởi vì const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
8 có thể được gán trực tiếp với function Shape[] {}
function Rectangle[] {
Shape.call[this]; // call super constructor.
}
Rectangle.prototype = Object.create[Shape.prototype];
Rectangle.prototype.constructor = Rectangle;
const rect = new Rectangle[];
rect instanceof Object; // true
rect instanceof Shape; // true
rect instanceof Rectangle; // true
rect instanceof String; // false
const literalObject = {};
const nullObject = Object.create[null];
nullObject.name = "My object";
literalObject instanceof Object; // true, every object literal has Object.prototype as prototype
[{} instanceof Object]; // true, same case as above
nullObject instanceof Object; // false, prototype is end of prototype chain [null]
1 làm nguyên mẫu của nó. Trong trường hợp này, nếu mã của bạn đọc các trường riêng của const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
9 từ const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
8, nó vẫn sẽ thất bại:// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
1Để tránh điều này, bạn có thể ghi đè hành vi của
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
5 bằng cách thêm phương thức // defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
7 vào const myDate = new Date[];
myDate instanceof Date; // true
myDate instanceof Object; // true
myDate instanceof String; // false
9, để nó kiểm tra thương hiệu với function Shape[] {}
function Rectangle[] {
Shape.call[this]; // call super constructor.
}
Rectangle.prototype = Object.create[Shape.prototype];
Rectangle.prototype.constructor = Rectangle;
const rect = new Rectangle[];
rect instanceof Object; // true
rect instanceof Shape; // true
rect instanceof Rectangle; // true
rect instanceof String; // false
const literalObject = {};
const nullObject = Object.create[null];
nullObject.name = "My object";
literalObject instanceof Object; // true, every object literal has Object.prototype as prototype
[{} instanceof Object]; // true, same case as above
nullObject instanceof Object; // false, prototype is end of prototype chain [null]
7:// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
2
Lưu ý rằng bạn có thể muốn giới hạn hành vi này trong lớp hiện tại; Nếu không, nó có thể dẫn đến dương tính giả cho các lớp con:
// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
3Bạn có thể làm điều này bằng cách kiểm tra rằng
// This class allows plain objects to be disguised as this class's instance,
// as long as the object has a particular flag as its property.
class Forgeable {
static isInstanceFlag = Symbol["isInstanceFlag"];
static [Symbol.hasInstance][obj] {
return Forgeable.isInstanceFlag in obj;
}
}
const obj = { [Forgeable.isInstanceFlag]: true };
console.log[obj instanceof Forgeable]; // true
1 là hàm tạo hiện tại:// defining constructors
function C[] {}
function D[] {}
const o = new C[];
// true, because: Object.getPrototypeOf[o] === C.prototype
o instanceof C;
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D;
o instanceof Object; // true, because:
C.prototype instanceof Object; // true
// Re-assign `constructor.prototype`: you should
// rarely do this in practice.
C.prototype = {};
const o2 = new C[];
o2 instanceof C; // true
// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C;
D.prototype = new C[]; // add C to [[Prototype]] linkage of D
const o3 = new D[];
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain
4Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec Rational-Operators # sec-relational-operators |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt