This is the 15th day of my participation in the August More Text Challenge. For details, see:August is more challenging

preface

Hello, everyone, today to record the javascript array, string and value reference, the content of the main reference “you don’t know javascript” (volume), at the same time will add their own thinking, as well as practice examples, it is said that a good memory is not as bad as writing, reading the book must be frequently take notes, so that not only remember firmly, I can also improve myself.

In addition, in advance of oh, this is a front note of the article, belongs to personal records. Please take a detour not to spray, thank you ~

The body of the

An array of

When you delete an array element with a subscript using the delete operator, the subscript is not removed and the element is replaced by an empty unit, so the length property of the array does not change. Such as:

Similarly, we can create an array with empty cells, such as:

An array is a type of object, so it can also hold key-value pairs, but the key-value pairs don’t count toward the length of the array, but if the key can be strongly converted to an int(without decimals), it counts toward the length of the array, and the key is treated as an index index.

Classarrays: For example, arguments and the obtained DOM element list are classarrays. A classarray is actually a set of numerically indexed values. Often use Array. Prototype. Slice. Call (the arguments) or an Array. The from (the arguments) to implement the class Array is converted into an Array.

string

A string has a length attribute, so STR [n] can be used for a character subscript n, but the correct method is str.charat (n).

Strings are immutable, and the member functions of a string do not change their original values, but instead create and return a new string, unlike the member functions of an array that operate on their original values.

A string can borrow from an array member function, such as:

Array. The prototype. Join the call (STR) said the STR to borrow the join method of Array.

Value and reference

When assigning/passing in js, simple values are assigned/passed by copying, and compound values are assigned/passed by copying.

Simple values: NULL,undefined, string, number, Boolean, symbol in ES6.

Compound values: objects (including arrays and encapsulated objects), functions.

A compound value can have multiple references, and changes made by each reference affect the results of all the references because they refer to the same value. Simple values have only one reference, so the change applies only to the current reference.

A parameter is also a reference, such as foo(x). If b is passed as a compound value, it is passed as a reference, equivalent to x=b, not foo(a).

Let a =[1,2,3] foo(a) function foo(x){x.ush (4) console.log(a) // [1,2,3,4] x=[ Console.log (a) // [1,2,3,4] x.ush (5,6,7,8) return x // [5,6,7,8]}Copy the code

Function passing a variable of type a with a simple value does not change a, for example

let a = 1
foo(a)
function foo(x){
    x = x*2
    console.log(x) // 2
    console.log(a) // 1
}
Copy the code

To pass in the composition of a function that does not change, but only x within the function, you should make a copy of the composition.

To pass simple value changes to a function, encapsulate them into a compound value and pass them in.

Afterword.

Hello wow, I am the Antarctic ice, a technology and appearance level is proportional to the front end of the engineer, advocating to nail down the front end of the problem, I hope my blog has helped you.

Pay attention to me, the front road together. Hey ~ 😛