JavaScript exploration: Prototypechain

Source: Internet
Author: User
A prototype object is also a common object and may have its own prototype. If the prototype of a prototype object is not nu

A prototype object is also a common object and may have its own prototype. If the prototype of a prototype object is not null, we call it a prototype chain ).

A prototype chain is a finite chain of objects which is used to implemented inheritance and shared properties.

Prototype chain is a finite object chain composed of objects because of its inheritance and sharing attributes.

Imagine a situation where most of the two objects are the same and only a small part is different. Obviously, in a good design model, we need to reuse the same part, instead of repeatedly defining the same methods or attributes in each object. In A system based on class [class-based], these reusable parts are called class inheritance-put the same part into class A, and then class B and class C inherit from class, you can declare that you have something unique.

ECMAScript does not have the concept of a class. However, the concept of reusing [reuse] is no different (in some aspects, or even more flexible than class-) and can be implemented by prototype chain. This inheritance is called delegation based inheritance-based delegation, or more commonly, prototype inheritance.

Similar to "A", "B", and "C", create an object class "a", "B", and "c" in ECMAScript. Correspondingly, the object "a" has a common part of the object "B" and "c. Meanwhile, objects "B" and "c" only contain their own additional attributes or methods.

Var a = {x: 10, calculate: function (z) {return this. x + this. y + z }}; var B = {y: 20, _ proto __: a}; var c = {y: 30, _ proto __: }; // call the inherited method B. calculate (30); // 60c. calculate (40); // 80

It seems very easy. B and c can use the calculate method defined in a, which is implemented by the original type chain [prototype chain.

The principle is simple: if the calculate method cannot be found in object B (that is, the calculate attribute is not found in object B), it will be searched along the prototype chain. If the calculate method is not found in prototype of B, the prototype of a is found along the prototype chain and the entire prototype chain is traversed. Remember, once found, the first property or method is returned. Therefore, the first property is an inherited property. If the entire prototype chain is still not found, undefined is returned.

Note that this value still points to the object it originally belongs to in an inheritance mechanism, rather than the object it belongs to when it is found on the prototype chain. For example, in the above example, this. y is obtained from B and c, not. Of course, you also found that this. x is obtained from a, because it is found through the prototype chain mechanism.

If the prototype of an object has not been declared or defined, the default value of _ prototype _ is object. prototype, and object. prototype also has a _ prototype __, which is the end point of the prototype chain and is set to null.

The following figure shows the inheritance relationships of a, B, and c:

Prototype chain

A prototype chain is usually used in this case: the object has the same or similar state structure (same or similar state structure) (that is, the same property set) different Status values (different state values ). In this case, we can use Constructor to create an object in a specific mode (specified pattern.

Additional reading

The topic list of this article is as follows:

  1. How should we understand the working principle of the JavaScript engine?
  2. JavaScript exploration: the importance of writing maintainable code
  3. JavaScript exploration: exercise caution when using global variables
  4. JavaScript exploration: var pre-parsing and side effects
  5. JavaScript exploration: for Loop (for Loops)
  6. JavaScript exploration: for-in loop (for-in Loops)
  7. Exploring JavaScript: Prototypes is too powerful
  8. JavaScript: eval () is the devil"
  9. JavaScript exploration: Using parseInt () for Numerical Conversion
  10. Exploring JavaScript: Basic coding specifications
  11. JavaScript exploration: function declaration and function expression
  12. JavaScript exploration: Name function expressions
  13. JavaScript: function name in the debugger
  14. JavaScript: JScript Bug
  15. JavaScript exploration: Memory Management of JScript
  16. Exploring JavaScript: SpiderMonkey's quirks
  17. JavaScript exploration: an alternative solution to naming function expressions
  18. JavaScript exploration: Object
  19. JavaScript exploration: Prototype chain
  20. JavaScript exploration: Constructor
  21. JavaScript probing: executable context Stack
  22. Execution context 1: Variable object and activity object
  23. Execution context 2: Scope chain Scope Chains
  24. Execution context 3: Closure Closures
  25. Execution context 4: This pointer
  26. Exploring JavaScript: Powerful prototype and prototype chain
  27. JavaScript Functions 1: function declaration
  28. JavaScript function 2: function expressions
  29. JavaScript function 3: function expressions in a group
  30. JavaScript function 4: function Constructor
  31. JavaScript variable object 1: VO Declaration
  32. JavaScript variable object 2: VO in different execution contexts
  33. JavaScript variable object 3: two stages of execution Context
  34. JavaScript variable object IV: Variables
  35. Property of the JavaScript variable object __parent _
  36. JavaScript scope chain 1: Scope chain Definition
  37. JavaScript scope chain 2: function Lifecycle
  38. JavaScript scope chain 3: Scope chain features
  39. JavaScript closure 1: Introduction to closures
  40. JavaScript closure 2: Implementation of closure
  41. JavaScript closure 3: Closure usage

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1641,welcome.

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.