| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/instanceof | [Back] [Original] |
Get to know MDN better
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
object instanceof constructor
TypeErrorconstructor constructor [Symbol.hasInstance]()
instanceof object constructor.prototype objectconstructor
//
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
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
class Base {}
const BoundBase = Base.bind(null, 1, 2);
console.log(new Base() instanceof BoundBase); // true
constructor Symbol.hasInstance object this constructor
//
//
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
JavaScript[] instanceof window.frames[0].Array false Array.prototype !== window.frames[0].Array.prototype
Node SVGElement myNode instanceof myNode.ownerDocument.defaultView.SVGElement
instanceof String
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
const myMap = new Map();
myMap instanceof Map; // true
myMap instanceof Object; // true
myMap instanceof String; // false
instanceof Object.create()
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)
Car myCar instanceof myCar Car Object
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
if (!(myCar instanceof Car)) {
//
// myCar = new Car(myCar)
}
if (!myCar instanceof Car) {
//
}
false !myCar instanceof Car
instanceof x instanceof C x C x C.prototype x C
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
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));
}
class D extends C {}
console.log(new C() instanceof D); // true : D C [Symbol.hasInstance]
this
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 |