Reference scope closure context usage

Source: Internet
Author: User

Reference, scope, closure, and context are the top priorities of JavaScript and the foundation for learning JavaScript. Here I will share with you a simple understanding:

1. First, describe the reference (Definition): The reference is a pointer to the actual position of an object;

Note: here we may not understand this very well. To put it simply, "Reference" = "alias ". (I think) in programming languages, variables, objects ..... And so on.

The variable is also an object. The following uses a simple example of a variable as an example.

For example, if there is a variable a, you want to reference it next time and give it an alias B;

 

Int;

Int & B = a; // remember: pointer to the actual position of the object. (Hey hey... If you have a good understanding of pointers, you don't need to read the following code. You can't do programming)


Note 1: Here is a reference to the variable. The memory does not open up the actual memory unit for it. The B and a pairs represent the same variable unit. When declaring a reference, it must be initialized at the same time. (Only declarations are not defined, but only relations between them and an existing variable. When a is referenced, the values a and B change with one of them at the same time ).

NOTE 2: After you declare a reference (B), the reference is always associated with the variable it represents and cannot be used as an alias for other variables (one-to-one correspondence ).

 

Ii. Learn about references. The following describes the purpose of introducing references:

One sentence: use it as a function parameter to expand the function's ability to transmit data. (In the next article, the parameters are passed in detail)

Here is a phenomenon that I do not understand at present (I hope readers will be grateful for it ):

The reference is a pointer to the actual position of the object, but it appears in JavaScript (not in the C ++ language, and the plaintext specifies that the referenced array cannot be created) cause (individual): the array name only represents the first address of the array. It is not a variable that occupies the bucket.

For example:

/// Create an array object

Var items new {items1, items 2, items 3 };

Var itemRef = items; // array Object Reference

// Add an element to the array

Items. push ("items4 ");

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.