| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/instanceof | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julho de 2015.
O operador instanceof testa se um objeto tem, em seu prototype, a funo construtora.
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);
// Expected output: true
console.log(auto instanceof Object);
// Expected output: true
objeto instanceof construtor
objetoO objeto a ser testado
construtorFuno construtora a ser verificada
O operador instanceof testa a presena da funo construtora no prototype do objeto.
// definindo construtores
function C() {}
function D() {}
var o = new C();
// true, porque: Object.getPrototypeOf(o) === C.prototype
o instanceof C;
// false, porque D.prototype no est no prototype desse objeto
o instanceof D;
o instanceof Object; // true, porque:
C.prototype instanceof Object; // true
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
// false, porque C.prototype no est mais no prototype desse objeto
o instanceof C;
D.prototype = new C(); // use inheritance
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true
Note que o resultado do instanceof pode alterar quando a gente altera o prototype da funo construtora. No entanto, a gente no pode alterar (por padro) o prototype do objeto. S possvel fazer essa alterao usando a pseudopropriedade __proto__.
instanceof and multiple context (e.g. frames or windows)Different scope 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)
Nota:
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 xpcomInterfaceworks as expected even if obj is from a different scope.
String and Date are of type Object and exceptional casesThe 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
mycar is of type Car and type ObjectThe 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; // retorna true
var b = mycar instanceof Object; // retorna true
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-relational-operators |
This page was last modified on 12 de out. de 2025 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |