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 ");