Basic knowledge of _ proto _ attribute _ in JavaScript

Source: Internet
Author: User
This article mainly introduces the _ proto _ attribute in JavaScript. For the so-called object in JavaScript, it points to the prototype of the object, if you need it, you can refer to JavaScript as an object-oriented speech, that is, everything is an object.

So how to generate an object? In the Java World, objects are produced by Class instances. In other words, objects are abstracted into a mold using this mold (Class) produce specific objects ).

However, JS does not have the concept of class. Some are "prototype", and objects are derived from prototypes. In general, in the Javascript world, "prototype" is not a mold, but a specific object ). All objects are derived from another object, and the derived object is the so-called "prototype object ".

There are three types of objects in javascript: one object created by the user, two constructor objects, and three prototype objects.

  • The object created by the user is generally explicitly constructed using the new statement.
  • The object of the constructor. A common constructor is a function that generates a common object by calling new.
  • The prototype object that the constructor points.

Each class of these three objects has a property-_ proto _, which points to the prototype of the Object and can be traced back to the Object from any Object that follows it. prototype.

Constructor has a prototype object that points to a prototype object. when an object is created using this constructor, the _ proto _ attribute of the created object points to the prototype attribute of the constructor.

The prototype object has a constructor attribute pointing to its corresponding constructor.

Talk is cheap, show me the code! Let's take a look at the Code:

var obj = {};console.log(obj);

Let's take a look at _ proto _: some default methods.

The _ proto _ OBJECT also has a _ proto _ OBJECT. As we just mentioned, each object has a _ proto _ attribute pointing to its prototype object. Let's print the _ proto __in _ proto __:

console.log(obj.__proto__.__proto__); //--> null

The result is null, indicating that the top-level prototype object has been reached. Obj is defined by braces {}. The prototype object of obj is naturally the top-level object of JS.

Let's look at one end of the Code to enhance our understanding:

var parent = {  name : "parent"};var child = {  name : "child",  __proto__ : parent};var subChild = {  name : "subChild",  __proto__ : child}console.log(subChild);

  • SubChild. _ proto _ --> child
  • Child. _ proto _ --> parent
  • Parent. _ proto _ --> top-level prototype object
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.