In-depth description of typeof and instanceof in JavaScript

Source: Internet
Author: User
Tags hasownproperty

In-depth description of typeof and instanceof in JavaScript

Understanding prototype

A prototype is an object that can be used to inherit attributes. Any object can be inherited. All objects have a prototype by default. Because the prototype itself is also an object, each prototype has another prototype. Any object has a prototype attribute, which is marked as :__ proto __. Every time we define an object, its _ proto _ attribute points to its prototype. Example:

 
 
  1. var foo = { 
  2. x: 10, 
  3. y: 20 
  4. }; 

This attribute is reserved even if prototype is not specified. If we have a clear point, the linked list will be connected. Note that prototype also points to the highest level of object. prototype. Example:

 
 
  1. var a = { 
  2. x: 10, 
  3. calculate: function (z) { 
  4. return this.x + this.y + z 
  5. }; 
  6. var b = { 
  7. y: 20, 
  8. __proto__: a 
  9. }; 
  10.  
  11. var c = { 
  12. y: 30, 
  13. __proto__: a 
  14. }; 
  15.  
  16. // call the inherited method 
  17. b.calculate(30); // 60 

Use prototype

After understanding the prototype principle, how can we use the prototype? Or what is the role of prototype?

Generally, after learning the basic javascript syntax, beginners are programming through functions. The following code:

 
 
  1. var decimalDigits = 2, 
  2. tax = 5; 
  3.  
  4. function add(x, y) { 
  5.     return x + y; 
  6.  
  7. function subtract(x, y) { 
  8.     return x - y; 
  9.  
  10. //alert(add(1, 3)); 

Execute each function to get the final result. However, using the prototype, We can optimize some of our code and use the constructor:

First, only variables are stored in the function ontology:

 
 
  1. var Calculator = function (decimalDigits, tax) { 
  2.     this.decimalDigits = decimalDigits; 
  3.     this.tax = tax; 
  4. }; 

The specific method is set through the prototype attribute:

 
 
  1. Calculator.prototype = { 
  2.     add: function (x, y) { 
  3.         return x + y; 
  4.     }, 
  5.  
  6.     subtract: function (x, y) { 
  7.         return x - y; 
  8.     } 
  9. }; 
  10. //alert((new Calculator()).add(1, 3)); 

In this way, function operations can be performed after the object is instantiated. This is also the method used by the general js framework.

Another role of prototype is to implement inheritance. First, define the parent object:

 
 
  1. var BaseCalculator = function() { 
  2.     this.decimalDigits = 2; 
  3. }; 
  4.  
  5. BaseCalculator.prototype = { 
  6.     add: function(x, y) { 
  7.         return x + y; 
  8.     }, 
  9.     subtract: function(x, y) { 
  10.         return x - y; 
  11.     } 
  12. }; 

Define the sub-object and point the prototype of the sub-object to the instantiation of the parent element:

 
 
  1. Var Calculator = function (){
  2. // Declare a tax number for each instance
  3. This. tax = 5;
  4. };
  5.  
  6. Calculator. prototype = new BaseCalculator ();

We can see that the prototype of Calculator is directed to an instance of BaseCalculator, so that Calculator can integrate its add (x, y) and subtract (x, y) functions, another point is that, since its prototype is an instance of BaseCalculator, no matter how many Calculator object instances you create, their prototype points to the same instance.

After running the code above, we can see that the Calculator prototype is directed to the BaseCalculator instance, so we can access its decimalDigits attribute value, what if I don't want Calculator to access the attribute value declared in the BaseCalculator constructor? You only need to point Calculator to the BaseCalculator prototype instead of the instance. The Code is as follows:

 
 
  1. var Calculator = function () { 
  2.     this.tax= 5; 
  3. }; 
  4.  
  5. Calculator.prototype = BaseCalculator.prototype; 

When using third-party libraries, sometimes the prototype method they define cannot meet our needs, we can add some methods by ourselves. The Code is as follows:

 
 
  1. // Overwrite the add () function of the previous Calculator.
  2. Calculator. prototype. add = function (x, y ){
  3. Return x + y + this. tax;
  4. };
  5.  
  6. Var calc = new Calculator ();
  7. Alert (calc. add (1, 1 ));

Prototype chain

The prototype of an object points to the parent of an object, and the prototype of a parent points to the parent. This prototype is called a prototype chain.

When you look for an Object's properties, javascript will traverse the prototype chain up until the property of the given name is found. When the search reaches the top of the prototype chain, that is, the Object. prototype. If the specified property is still not found, undefined is returned.

Example:

 
 
  1. Function foo (){
  2. This. add = function (x, y ){
  3. Return x + y;
  4. }
  5. }
  6.  
  7. Foo. prototype. add = function (x, y ){
  8. Return x + y + 10;
  9. }
  10.  
  11. Object. prototype. subtract = function (x, y ){
  12. Return x-y;
  13. }
  14.  
  15. Var f = new foo ();
  16. Alert (f. add (1, 2); // The result is 3 instead of 13
  17. Alert (f. subtract (1, 2); // The result is-1

We can find that subtrace is based on the upward search principle, while add is an exception. The reason is that when you search for a property, you first find its own property. If no prototype is found.

When it comes to Object. prototype, you have to mention its method, hasOwnProperty. It can determine whether an object contains custom attributes rather than the attributes on the prototype chain. It is the only function in javascript that processes attributes but does not search for the prototype chain. The Code is as follows:

 
 
  1. // Modify Object. prototype
  2. Object. prototype. bar = 1;
  3. Var foo = {goo: undefined };
  4.  
  5. Foo. bar; // 1
  6. 'Bar' in foo; // true
  7.  
  8. Foo. hasOwnProperty ('bar'); // false
  9. Foo. hasOwnProperty ('goo'); // true

To determine the relationship between a prototype object and an instance, we have to introduce the isPrototyleOf method, as shown below:

 
 
  1. alert(Cat.prototype.isPrototypeOf(cat2)); //true 

 

 

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.