JavaScript instanceof Operator Learning Tutorial _ Basics

Source: Internet
Author: User
Tags stringr

Grammar

Object Instanceof Constructor

Parameters
Object
The object to be instrumented.
Constructor:
A constructor

Describe:
The instanceof operator is used to detect whether the Constructor.prototype exists on the prototype chain of parameter object.

Define constructor function function
C () {} 
function D () {} 

var o = new C ();

True because Object.getprototypeof (o) = = = C.prototype
o instanceof C; 

False because D.prototype is not on the prototype chain o
instanceof D; 

o instanceof Object; True because Object.prototype.isPrototypeOf (o) returns true
c.prototype instanceof Object//True, ditto

c.prototype = {} ;
var O2 = new C ();

O2 instanceof C; True

o instanceof C;//False,c.prototype points to an empty object that is not on the prototype chain of O.

D.prototype = new C (); Inherit
var O3 = new D ();
O3 instanceof D; True
O3 instanceof C;//True

It should be noted that if the expression obj instanceof Foo returns True, it does not mean that the expression will return to Ture forever, Because the value of the Foo.prototype property is likely to change, the value after the change is likely to not exist on the prototype chain of obj, when the value of the original expression becomes false. In another case, the value of the original expression will also change, that is, to change the prototype chain of object obj, although in the current ES specification, we can only read the object's prototype and not change it, but with the help of non-standard __proto__ magic attributes, can be achieved. For example, after executing obj.__proto__ = {}, obj instanceof Foo returns false.

Instanceof and multiple global objects (interaction between multiple frame or multiple windows)

In the browser, our script may need to interact with multiple windows. Multiple windows imply multiple global environments, and different global environments have different global objects that have different built-in type constructors. This may cause some problems. For example, an expression [] instanceof window.frames[0]. Array returns false because Array.prototype!== window.frames[0]. Array.prototype, so you must use Array.isarray (myobj) or Object.prototype.toString.call (myobj) = = "[Object Array]" To determine whether MyObj is an array.

Example
the general use of instanceof is to determine whether a is a B-type:

Console.log (True instanceof Boolean); False 
Console.log (new number (1) instanceof number);//True

Instanceof can also determine the parent type:

function Father () {}
function child () {}
Child.prototype = new Father ();
var a = new Child ();
Console.log (a instanceof child); True
Console.log (a instanceof Father);//True

The child constructor inherits from father, and instance A is no doubt constructed by the child, but why is it also an instance of Father? In fact, the kernel of the instanceof operator can be simply described in the following code:

function Check (A, b) {while
 (a.__proto__) {
  if (a.__proto__ = = B.prototype) return
   true;
  A = a.__proto__;
 }
 return false;
}

function Foo () {}
Console.log (object instanceof Object = = Check (object, object);//True 
Console.log ( function instanceof function = = Check (function, function)); True 
console.log (number instanceof number = = Check (number, number));//True 
Console.log (String instanceof String = = Check (string, string)); True 
console.log (function instanceof Object = = Check (function, Object);//True 
Console.log (Foo instanceof Function = = Check (Foo, Function)); True 
console.log (foo instanceof foo = = Check (foo, foo));//True

Simply put, if a is an instance of B, then a must be able to use the methods and attributes defined in the prototype of B, then the code representation is the same object in the prototype chain of a, which has the B.prototype value, and then it follows a layer of the prototype chain of a.

It is also noteworthy that String number Boolean and function are functions, and functions are unified by function constructs, so they, like any simple function, can use the stereotype properties on function:

Function.prototype.a = ten;
Console.log (STRING.A); 10

Finally, let's talk about the first two questions.

 To facilitate presentation, first distinguish between left and right expressions
 functionl = function, Functionr = function; 
 Below according to the specification step-by-Step deduction
 O = Functionr.prototype = Function.prototype 
 L = functionl.__proto__ = Function.prototype 
 // First judgment
 O = = L 
 //return TRUE//

to facilitate presentation, first differentiate between left and right expressions
 Stringl = string, Stringr = string; 
 Below according to the specification step-by-Step deduction
 O = Stringr.prototype = String.prototype 
 L = stringl.__proto__ = Function.prototype 
 // First Judge
 O!= L
 //loops find again l 
 = __proto__ L = string.prototype.__proto__ = Object.prototype 
 //second judgment 
   o!= L 
 //re-loop find l 
 = __proto__ L = string.prototype.__proto__ = null 
 //Third judgment
 L = = null 
 // return False

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.