JavaScript operator instanceof

Source: Internet
Author: User

Overview

instanceof Operator can be used to determine the function of a constructorprototype属性是否存在另外一个要检测对象的原型链上。

Grammar
instanceof Constructor
Parameters
object
the object to be detected.
constructor
a constructor
Describe

instanceofoperator is used to detect the constructor.prototype existence object of a prototype chain on a parameter.

functionC () {}//define a constructorfunctionD () {}//define another constructorvarn \NewC (); OinstanceofC//true because: object.getprototypeof (o) = = = C.prototypeOinstanceofD//false, because D.prototype is not on the prototype chain of OOinstanceofObject;//true, because Object.prototype.isPrototypeOf (o) returns TrueC.prototypeinstanceofObject//true, Ibid.C.prototype= {};varO2 =NewC (); O2instanceofC//trueOinstanceofC//False,c.prototype points to an empty object that is not on the prototype chain of O.D.prototype=NewC ();varO3 =NewD (); O3instanceofD//trueO3instanceofC//true

It should be noted that if the expression obj instanceof Foo 返回true,则并不意味着该表达式会永远返回ture,因为Foo.prototype属性的值有可能会改变 , the value after the change is likely to not exist in obj the prototype chain, then the value of the original expression will become false . In another case, the value of the original expression will change, that is, changing the obj prototype chain of the object, although in the current ES specification, we can only read the prototype of the object can not change it, but with the help of non-standard __proto__魔法属性 , 是可以实现的 . For example 执行obj.__proto__ = {}之后 ,obj instanceof Foo就会返回false了。

instanceof和多全局对象(Interaction between multiple frame or multiple windows)

In the browser, our script may need to interact between multiple windows. Multiple windows mean multiple global environments, and different global environments have different global objects, with different built-in type constructors. This may cause some problems. For example, the expression [] instanceof window.frames[0].Array returns false , because Array.prototype !==  window.frames[0].Array .prototype , 因此你必须使用 Array.isArray(myObj)或者 Object.prototype.toString.call(myObj) === "[object Array]" to determine whether MyObj is an array.

Example Example: Indicates StringObjects, and Date对象都属于 Object类型

The following code is used instanceof to prove: String和 Date对象 also belongs to Object类型 .

varMyString =NewString ();varMyDate =NewDate (); MyStringinstanceofString;//returns TrueMyStringinstanceofObject;//returns TrueMyStringinstanceofDate;//returns falsemydateinstanceofDate;//returns TrueMyDateinstanceofObject;//returns TrueMyDateinstanceofString;//returns false
Example: Indicates mycar属于 Car类型,同时又Belong to Object类型

The following code creates a type Car 以及该类型的对象实例 mycar . instanceof operator indicates this mycar对象既属于 Car类型 , 又属于 Object类型 .

function Car (make, model, year) {  the. Make = make ;    this. Model = model;   this. Year = year ;} var New Car ("Honda", "Accord", 1998); var instanceof Car;    // returns True var instanceof // returns True
Specification
Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.4
ECMAScript 5.1 (ECMA-262)
The instanceof operator
Standard
ECMAScript 6 (ECMA-262)
Relational Operators
Draft
Browser compatibility
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic Support (Yes) (Yes) (Yes) (Yes) (Yes)

JavaScript operator instanceof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.