Hướng dẫn what is instance of an object in javascript? - thể hiện của một đối tượng trong javascript là gì?

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
1 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
2 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
3.
// 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 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
2 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
3.

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
4

Đố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
5

Người xây dựng để kiểm tra chống lại.

Ngoại 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
6

Né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
5 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
5 không 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
9, 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
1 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;
1 trong chuỗi nguyên mẫu của ____ 14. Điều này thường 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
4 đượ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
5.

// 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
1 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;
1 đượ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 ____ 14 bằng cách sử dụng
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;
8.

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
2.

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;

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
5 có phương pháp
// 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
3, 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
4 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
5 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
4.

// 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

Đố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
1 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
2 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
2.

class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // 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
8 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
9, bởi vì
class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
0 và các mảng trong vương quốc hiện tại kế thừa từ 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

class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
1, bỏ qua phạm vi nào nó đến từ.

Ví dụ: để kiểm tra xem

class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
2 có phải là
class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
3 trong bối cảnh khác không, bạn có thể sử dụng
class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
4.

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
1 với các đối tượng
class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
6.

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
1 với các đối tượng
class Base {}
const BoundBase = Base.bind[null, 1, 2];
console.log[new Base[] instanceof BoundBase]; // true
8.

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
1 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
0.

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
1 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
2. 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
1 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
2 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
1 và 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
6.

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
9. .

Thông số kỹ thuật

Sự chỉ rõ
Đặ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

Xem thêm

Một ví dụ của đối tượng là gì?

Một ví dụ là gì? Trong lập trình hướng đối tượng [OOP], một trường hợp là một nhận thức cụ thể của bất kỳ đối tượng nào. Một đối tượng có thể khác nhau theo nhiều cách và mỗi biến thể nhận ra của đối tượng đó là một ví dụ. Việc tạo ra một ví dụ được thực hiện được gọi là khởi tạo.a specific realization of any object. An object may be different in several ways, and each realized variation of that object is an instance. The creation of a realized instance is called instantiation.

Ví dụ của một lớp trong JavaScript là gì?

Một thể hiện là một đối tượng chứa dữ liệu và hành vi được mô tả bởi lớp.Toán tử mới khởi tạo lớp trong javascript: instance = new Class [].Ví dụ: bạn có thể khởi tạo lớp người dùng bằng toán tử mới: const myuser = new user [];Người dùng mới [] tạo một thể hiện của lớp người dùng.an object containing data and behavior described by the class. The new operator instantiates the class in JavaScript: instance = new Class[] . For example, you can instantiate the User class using the new operator: const myUser = new User[]; new User[] creates an instance of the User class.

Một biến thể hiện trong JavaScript là gì?

Một đối tượng là một thể hiện của lớp mà nó được tạo từ.Và một biến thể hiện là bất kỳ thuộc tính nào được xác định trong lớp cho đối tượng đó [thường là bên trong hàm tạo].Mỗi biến thể hiện có thể truy cập được bằng cách sử dụng 'Điều này.'Tiền tố bên trong bất kỳ phương thức của lớp.any property defined in the class for that object [typically inside constructor]. Every instance variable is accessible using 'this. ' prefix inside any method of the class.

Ví dụ của toán tử làm gì trong JavaScript?

Toán tử InstanceOf kiểm tra để xem thuộc tính nguyên mẫu 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.tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

Bài Viết Liên Quan

Chủ Đề