JS magic Hall: learn more about instanceof

Source: Internet
Author: User

JS magic Hall: learn more about instanceof
I. Breif we all know that instanceof is generally used to check whether the object A is an instance of Class B or A subclass. The problem is that JS does not have the concept of class, nor does it have the concept of class inheritance (although there are constructors). So how does instanceof determine that object A is a B constructor instance? This article will make analysis records for future reference. 2. Reference 2 ECMA-262-3 Spec http://bclary.com/2004/11/07/#a-11.8.6 The production RelationalExpression: RelationalExpression instanceof ShiftExpression is evaluated as follows: 1. evaluate RelationalExpression. 2. call GetValue (Result (1 )). 3. evaluate ShiftExpression. 4. call GetValue (Result (3 )). 5.If Result (4) is not an object, throw a TypeError exception. 6.If Result (4) does not have a [[HasInsta Nce] method, throw a TypeError exception. 7. call the [[HasInstance] method of Result (4) with parameter Result (2 ). 8. return Result (7 ). from the above definition, we can draw the following content: 1. the actual value of ShiftExpression (GetValue (Evaluate (ShiftExpression) must be [object Function]; otherwise, a TypeError exception is thrown; 2. the actual determination of instanceof is to call Internal Method [[HasInstance] of RelationalExpression for processing. Next let's take a look at [[HasInstance] definition http://bclary.com/2004/11/07/#a-15.3.5.3 Assume F is a Function object. when the [[HasInstance] method of F is called with value V, the following steps are taken: 1. if V is not an object, return false. 2. call the [[Get] method of F with property name "prototype ". 3. let O be Result (2 ). 4. if O is not an object, throw a TypeError exception. 5. let V be the value of the [[Prototype] property of V. 6. if V is null, return false. 7. if O and V refer to the same object or if they refer to objects joined to each other (13.1.2), return true. 8. go to step 5. the above definition is not quite clear. Let's translate it into JS implementation.

// IE5.5 ~ 9. This method is invalid because the Internal Property [[Prototype] of the object cannot be accessed through _ proto _; (function (rNotObj) {Function. prototype ['[[HasInstance]'] = function (value) {// 1. if V is not an object, return false if (rNotObj. test (typeof value) return false // 2. call the [[Get] method of F with property name "prototype" // 4. if O is not an object, throw a TypeError exception var O = this. prototype if (rNotObj. test (typeof O) throw TypeError () // 5. let V be the value of the [[Prototype] prototype of V // 6. if V is null, return false if (null = (value = value. _ proto _) return false // 7. if O and V refer to the same object // 8. go to step 5 return O = value | this ['[[HasInstance]'] (value )}} (/$ [^ of] // * not begin with o (bject) neither f (unction )*/))

 

To sum up a bit, the key points of the underlying operational mechanism of a instanceof B are as follows: 1. b's data type must be [object Function]; otherwise, a TypeError is thrown; 2. if a is a Primitive Value, false is directly returned. If a is of the Object data type, subsequent operations are performed. if and only when B. true is returned only when prototype is in prototype chain of a (because the Object. prototype. _ proto _ is null, so prototype chain is a finite linked list. why is the prototype ['[[HasInstance]'] implementation successful? Let's take a look at it. We can see that all Function _ proto _ points to Function by default. prototype, while Function. _ proto _ and Function. prototy Pe points to the same object. Both points to function Empty () {} In Chrome. Therefore, the attribute added to Function. protoype also appears in Function prototype chain. 4. About if they refer to objects joined to each other Objects Joined is actually the underlying optimization method recommended by Spec implementers (such as V8 and SpiderMonkey.
Function a () {function B () {} return B} var c = a () var d = a () // If the JavaScript Engine implements Objects Joined, then c = d returns true. Because Function B is defined in Function a, the underlying implementation can no longer generate a new Function object, reducing the consumption of space and time.

 

 

Related Article

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.