--- title: Operator instanceof slug: Web/JavaScript/Reference/Operators/instanceof tags: - Dokumentacja_JavaScript - Dokumentacje - JavaScript - Strony_wymagające_dopracowania - Wszystkie_kategorie translation_of: Web/JavaScript/Reference/Operators/instanceof original_slug: Web/JavaScript/Referencje/Operatory/Operator_instanceof ---
{{jsSidebar("Operators")}}

Operator instanceof sprawdza czy właściwość konstruktora prototype pojawia się gdziekolwiek w łańcuchu prototypowym obiektu.

{{EmbedInteractiveExample("pages/js/expressions-instanceof.html")}}

Składnia

object instanceof constructor

Parametry

object
Obiekt do testowania.
constructor
Funkcja przeciwko której testujemy.

Opis

Operator instanceof sprawdza obecność constructor.prototype w łańcuchu prototypowym obiektu object

// definiowanie konstruktorów
function C() {}
function D() {}

var o = new C();

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

// false, ponieważ D.prototype nie występuje w łańcuchu prototypowym o.
o instanceof D;

o instanceof Object; // true, ponieważ:
C.prototype instanceof Object // true

C.prototype = {};
var o2 = new C();

o2 instanceof C; // true

// false, ponieważ C.prototype nie ma już w łańcuchu prototypowym o
o instanceof C;

D.prototype = new C(); // add C to [[Prototype]] linkage of D
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true since C.prototype is now in o3's prototype chain

Note that the value of an instanceof test can change based on changes to the prototype property of constructors, and it can also be changed by changing an object prototype using Object.setPrototypeOf. It is also possible using the non-standard __proto__ pseudo-property.

instanceof and multiple context (e.g. frames or windows)

Different scopes have different execution environments. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected results. For instance, [] instanceof window.frames[0].Array will return false, because Array.prototype !== window.frames[0].Array and arrays inherit from the former.

This may not make sense at first but when you start dealing with multiple frames or windows in your script and pass objects from one context to another via functions, this will be a valid and strong issue. For instance, you can securely check if a given object is, in fact, an Array using Array.isArray(myObj)

For example checking if a Nodes is a SVGElement in a different context you can use myNode instanceof myNode.ownerDocument.defaultView.SVGElement

Note for Mozilla developers:
In code using XPCOM instanceof has special effect: obj instanceof xpcomInterface (e.g. Components.interfaces.nsIFile) calls obj.QueryInterface(xpcomInterface) and returns true if QueryInterface succeeded. A side effect of such call is that you can use xpcomInterface's properties on obj after a successful instanceof test. Unlike standard JavaScript globals, the test obj instanceof xpcomInterface works as expected even if obj is from a different scope.

Examples

Demonstrating that String and Date are of type Object and exceptional cases

The following code uses instanceof to demonstrate that String and Date objects are also of type Object (they are derived from Object).

However, objects created with the object literal notation are an exception here: Although the prototype is undefined, instanceof Object returns true.

var simpleStr = 'This is a simple string';
var myString  = new String();
var newStr    = new String('String created with constructor');
var myDate    = new Date();
var myObj     = {};

simpleStr instanceof String; // returns false, checks the prototype chain, finds undefined
myString  instanceof String; // returns true
newStr    instanceof String; // returns true
myString  instanceof Object; // returns true

myObj instanceof Object;    // returns true, despite an undefined prototype
({})  instanceof Object;    // returns true, same case as above

myString instanceof Date;   // returns false

myDate instanceof Date;     // returns true
myDate instanceof Object;   // returns true
myDate instanceof String;   // returns false

Demonstrating that mycar is of type Car and type Object

The following code creates an object type Car and an instance of that object type, mycar. The instanceof operator demonstrates that the mycar object is of type Car and of type Object.

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
var mycar = new Car('Honda', 'Accord', 1998);
var a = mycar instanceof Car;    // returns true
var b = mycar instanceof Object; // returns true

Specifications

Specification Status Comment
{{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}} {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-11.8.6', 'The instanceof operator')}} {{Spec2('ES5.1')}}  
{{SpecName('ES3', '#sec-11.8.6', 'The instanceof operator')}} {{Spec2('ES3')}} Initial definition. Implemented in JavaScript 1.4.

Browser compatibility

{{Compat("javascript.operators.instanceof")}}

See also