[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/instanceof [Back]  [Original]

instanceof - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

instanceof

Baseline

20157

instanceof prototype Symbol.hasInstance

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car("Honda", "Accord", 1998);

console.log(auto instanceof Car);
// : true

console.log(auto instanceof Object);
// : true

js
object instanceof constructor

object

constructor

TypeError

constructor constructor [Symbol.hasInstance]()

instanceof object constructor.prototype objectconstructor

js
// 
function C() {}
function D() {}

const o = new C();

// true : Object.getPrototypeOf(o) === C.prototype 
o instanceof C;

// false : D.prototype  o 
o instanceof D;

o instanceof Object; // true : ...
C.prototype instanceof Object; // true 

// `constructor.prototype` 
// 
C.prototype = {};
const o2 = new C();

o2 instanceof C; // true

// false : C.prototype  o 
// 
o instanceof C;

D.prototype = new C(); // C  D  [[Prototype]] 
const o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true : o3  C.prototype 

constructor.prototype instanceof Object.setPrototypeOf object

prototype

js
class A {}
class B extends A {}

const o1 = new A();
// true : Object.getPrototypeOf(o1) === A.prototype 
o1 instanceof A;
// false : B.prototype  o1 
o1 instanceof B;

const o2 = new B();
// true : Object.getPrototypeOf(Object.getPrototypeOf(o2)) === A.prototype 
o2 instanceof A;
// true : Object.getPrototypeOf(o2) === B.prototype 
o2 instanceof B;

instanceof prototype prototype

js
class Base {}
const BoundBase = Base.bind(null, 1, 2);
console.log(new Base() instanceof BoundBase); // true

instanceof Symbol.hasInstance

constructor Symbol.hasInstance object this constructor

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

Function.prototype Function.prototype[Symbol.hasInstance]() instanceof instanceof Symbol.hasInstance

instanceof

JavaScript[] instanceof window.frames[0].Array false Array.prototype !== window.frames[0].Array.prototype

Array.isArray()

Node SVGElement myNode instanceof myNode.ownerDocument.defaultView.SVGElement

instanceof String

instanceof String

js
const literalString = "";
const stringObject = new String(" String ");

literalString instanceof String; // false :  String 
stringObject instanceof String; // true

literalString instanceof Object; // false :  Object 
stringObject instanceof Object; // true

stringObject instanceof Date; // false

instanceof Map

instanceof Map

js
const myMap = new Map();

myMap instanceof Map; // true
myMap instanceof Object; // true
myMap instanceof String; // false

Object.create()

instanceof Object.create()

js
function Shape() {}

function Rectangle() {
  Shape.call(this); // 
}

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 :  Object.prototype 
({}) instanceof Object; // true : 
nullObject instanceof Object; // false :  (null)

myCar Car Object

Car myCar instanceof myCar Car Object

js
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; // true 
const b = myCar instanceof Object; // true 

instanceof

instanceof

js
if (!(myCar instanceof Car)) {
  // 
  // myCar = new Car(myCar)
}

js
if (!myCar instanceof Car) {
  // 
}

false !myCar instanceof Car

instanceof

instanceof x instanceof C x C x C.prototype x C

js
class C {
  #value = "foo";
  static getValue(x) {
    return x.#value;
  }
}

const x = { __proto__: C.prototype };

if (x instanceof C) {
  console.log(C.getValue(x)); // TypeError: Cannot read private member #value from an object whose class did not declare it
}

C Symbol.hasInstance instanceof in

js
class C {
  #value = "foo";

  static [Symbol.hasInstance](x) {
    return #value in x;
  }

  static getValue(x) {
    return x.#value;
  }
}

const x = { __proto__: C.prototype };

if (x instanceof C) {
  // x  C 
  console.log(C.getValue(x));
}

js
class D extends C {}
console.log(new C() instanceof D); // true : D  C  [Symbol.hasInstance] 

this

js
class C {
  #value = "foo";

  static [Symbol.hasInstance](x) {
    return this === C && #value in x;
  }
}

class D extends C {}
console.log(new C() instanceof D); // false
console.log(new C() instanceof C); // true
console.log({ __proto__: C.prototype } instanceof C); // false

ECMAScript 2027 LanguageSpecification
# sec-relational-operators


Web Proxy Viewer  |  New URL  |  Original Page