Ambiguous relationship between the JavaScript prototype chain and the instanceof Operator

Source: Internet
Author: User
Tags stringr

Ambiguous relationship between the JavaScript prototype chain and the instanceof Operator

Two months ago, the prototype chain, prototype, and _ proto _ had a messy relationship. At the same time, I also briefly understood the typeof and instanceof operators. However, anyway, try the following two questions:

Console. log (Function instanceof Function );
Console. log (String instanceof String );

If you cannot get an accurate answer, follow the story of the landlord to learn new things.

Temperature

We often use the typeof operator to determine the type of a variable. It is quite helpful to determine the number, boolean, and string types, but the object judgment capability is average, for example, the results of Array and null are all objects, and the results of some new objects, such as new String () and new Number (), are all objects. In this way, there is a problem, instanceof cannot be used to determine object instances in more detail. Gu mingsiyi, instanceof is an operator used to determine whether an object is an constructor instance. In this way, whether the new variable is a number, string, or something else, now we have a further understanding of the judgment. It looks very simple. In fact, the above two questions are just a small test. Put them first, and then review the prototype chain.

In JavaScript, everything is an object, and all objects have a _ proto _ property value (prototype). It may not be supported by some browsers, the attribute value of an object is the prototype value of the constructor of the object. The function is to define a method for an object of a certain type without repeating the definition. For example, for an object [1, 2, 3], obviously this is an Array object. It has pop, push, and other methods, but it does not mean that it has these methods, but it is owned by its constructor Array Function, the value of _ proto _ of this object is the prototype value of the Array function. If so does not have a pop method, It will be searched from its _ proto, that is, the so-called prototype chain, and the end of the chain is the Object, because all objects are constructed by the Object, and the Object. prototype. _ proto _ specifies to point to null.

Text descriptions are always pale and powerless. For example:

 
 
  1. var fun = function(){ 
  2.   this.a = 1; 
  3. }; 
  4.   
  5. fun.prototype.b = 2; 
  6. var obj = new fun(); 
  7. obj.a; //1 
  8. obj.b; //2

The examples and figures of online piracy are as follows.

Zhixin

Next let's take a look at the instanceof operator.

The conventional usage of instanceof is to determine whether a is of the B type:

 
 
  1. console.log(true instanceof Boolean); // false  
  2. console.log(new Number(1) instanceof Number); // true 

Instanceof can also determine the parent type:

 
 
  1. function Father() {} function Child() {} Child.prototype = new Father(); var a = new Child(); console.log(a instanceof Child);  // true 
  2. console.log(a instanceof Father); // true 

The Child constructor inherits from Father. instance a is constructed by Child, but why is it also a Father instance? In fact, the kernel of the instanceof operator can be described in the following code:

 
 
  1. 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 
  2. console.log(Function instanceof Function === check(Function, Function)); // true 
  3. console.log(Number instanceof Number === check(Number, Number)); // true 
  4. console.log(String instanceof String === check(String, String)); // true 
  5. console.log(Function instanceof Object === check(Function, Object)); // true 
  6. console.log(Foo instanceof Function === check(Foo, Function)); // true 
  7. console.log(Foo instanceof Foo === check(Foo, Foo)); // true 

Simply put, if a is an instance of B, a will certainly be able to use the methods and attributes defined in prototype of B, so it is indicated by code that the prototype chain of a contains B. the prototype has the same value, so you can find the same prototype in the prototype chain of.

In addition, String Number Boolean and Function are all functions, while functions are constructed by functions in a unified manner. so they are the same as any simple functions, you can use the prototype attributes on the Function:

 
 
  1. Function.prototype.a = 10; console.log(String.a);  // 10 

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

 
 
  1. // For easy expression, first distinguish between the expression on the left and the expression on the right
  2. FunctionL = Function, FunctionR = Function; // the following code is used for deduction.
  3. O = FunctionR. prototype = Function. prototype L = FunctionL. _ proto _ = Function. prototype // first judgment
  4. O = L // return true
 
 
  1. // For easy expression, first distinguish between the expression on the left and the expression on the right
  2. StringL = String, StringR = String; // The following rule is used for deduction.
  3. O = StringR. prototype = String. prototype L = StringL. _ proto _ = Function. prototype // first judgment
  4. O! = L // search for whether L has _ proto _ again in a loop __
  5. L = String. prototype. _ proto _ = Object. prototype // second Judgment
  6. O! = L // search for whether L has _ proto _ again __
  7. L = String. prototype. _ proto _ = null
  8. // The third judgment
  9. L = null
  10. // Return false

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.