JavaScript Advanced Programming Learning Note 2

Source: Internet
Author: User
Tags array length

Garbage Collection principle:

Identify the variables that are no longer in use, and then release their memory.

The most commonly used garbage collection method in JS is tag cleanup , when variables enter the environment, the variables are marked as " go into the environment " and when the variables leave the environment, they are marked as "out of the environment " and finally the garbage collector completes the memory cleanup work.

Another less common garbage collection principle is the reference count, which keeps track of the number of times each value is referenced, and when the number of references is 0 o'clock, the garbage collector frees its memory, but there is a possibility of circular referencing , in which case you can manually set the reference to null. .

Web browsers typically have fewer available memory scores for desktop applications, so make sure that the least amount of memory is used to get better performance on the page, so only the necessary data is saved in the code, and once the data is no longer useful, it is best to manually set its value to NULL to release its reference-this is called de-reference .

Reference type:

Definition: A value (object) of a reference type is an instance of a reference type.

How to create an object instance:

The 1.new operator is followed by the object constructor.

2. Use object literal notation.

1 var person = {2     name: "Annika",3     age:294 }

Note: Separate attributes are separated by commas, and the last attribute does not add commas. The property name can also be used as a string. In the case of multiple optional parameters, a named parameter is used for the required value, and the object literal is used to encapsulate multiple optional parameters.

How to use: In general, it is the dot notation, or the square bracket notation, which writes the properties of the access to a string in square brackets.

The main advantage of square brackets is that the properties can be accessed through variables:

1 var propertyname = "name"; 2 alert (Person[propertyname]);     // "Annika"

You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name is in a keyword or reserved word:

person["First Name"]= "Annika";    The property name can contain non-alphabetic non-numeric

Array type:

ECMAScript differs from other languages in that it can hold any type of data.

Ways to create an array:

1.Array Constructors

1 var New Array (3);    // create an array with 3 items 2 var New Array ("Annika");  // creates an array of one item, that is, the string "Annika"

2. Array literal notation

1 var colors = ["Red", "Blue"];

The length property of the array is special--you can either remove the item from the end of the array or add a new item by setting this property.

method to detect that an object is not an array: Array.isarray ();

Conversion Method:(by default, comma-delimited, with the join () method, you can accept a delimiter as a parameter)

    • toLocaleString (),
    • ToString (), which returns each string form in the array
    • ValueOf (), returns the array itself

Stack method:

The stack is a LIFO (last-in-first-out) LIFO data structure where the insertion and removal of items in the stack occur at the top of the stack. There are two ways to ECMAScript in an array:

Push (): Pass in any parameter, add to the end of the array one by one, and return the modified array length.

Pop (): Removes the last item from the end of the array, reduces the length, and returns the item that is removed.

Queue method:

A queue is a Fifi (First-in-first-out) first-in-one data structure in which the queue adds items at the end and removes items at the front end of the list with two new methods:

Shift (): Removes the first item of the divisor group and returns the item.

Unshift (): Pushes any item from the front of the array and returns the array length.

Reorder methods:

Reverse (): Reverses the array order.

Sort (): By default, array items are sorted in ascending order, and the ToString () method of each array is called to compare the resulting string, so an unexpected answer can be obtained.

1 var values = [0, 1, 5, ten, +]; 2 Values.sort (); 3 alert (values);           // 0,1,10,15,5

So it's a good idea to add a parameter to sort:

1 functionCompare (value1,value2) {2     if(value1<value2) {3         return-1;4}Else if(Value1 <value2) {5         return1;6}Else {7         return0;8     }9}
1 Values.sort (compare); 2 alert (values);            // 0,1,5,10,15

Operation Method:

Cancat (): Creates a copy of the current array, adds the received parameters to the end of the copy, and returns the newly constructed array

Slice (): Do not change the original array, you can accept one to two parameters, only one parameter returns all items from this parameter to the end, if two, returns the entry between the start and end positions, if the argument is negative, the array length is called again, if the ending position is less than the starting position, an empty array is returned.

Splice (): Changes the original array and always returns the deleted item.

    • Delete: Just specify two parameters, the first item to delete, and the number of items to delete.
    • Insert: You can insert multiple items by simply providing 3 parameters, starting position, 0 (number of items to delete), and items to insert.
    • Replace: The specified parameter is the same as the insert, except that 0 can be changed to the number of items to be deleted.

Location Method:

A total of two location methods:

IndexOf (): From the beginning of the array

LastIndexOf (): From the end of the array

All accept two parameters, the item to find and the index of the starting position to find (optional), return 1 if not found, and use the congruent operator during the comparison.

Iterative methods:

There are 5 iterative methods, each of which accepts two parameters: the function to run on each item and the scope object that runs the function--the value that affects this, where there are three arguments to the passed-in function, the value of the array item, the position of the item in the array, and the group object itself.

Every (): Runs the given function for each item in the array, and returns True if the function returns true for each item.

1 var numbers = [1,2,3,4,5,4,3,2,1]23var everyresult = Numbers.every (  function(item,index,array) {4     return (item>2); 5 }); 6 7 alert (everyresult);     // false

Filter (): Runs the given function for each item in the array, and returns True if the function is returned.

1 var everyresult = Numbers.filter (function(item,index,array) {2     return (item>2); 3 }); 4 5 alert (everyresult);    // [3,4,5,4,3]

ForEach (): Each item of an array runs the specified function, and the method does not return a value.

1 numbers.foreach (function(item,index,array) {2     alert (item*2); 3 });    // 2,4,6,8,10,8,6,4,2

Map (): Each item in the array runs the given function, returning an array that consists of the results of each function call.

1 var mapresult = Numbers.map (function(item,index,array) {2     return Item*2; 3 }); 4 alert (mapresult);     // [2, 4, 6, 8, ten, 8, 6, 4, 2]

Some (): runs the specified function for each item in the array, and returns True if the function returns true for either item.

1 var someresult = Numbers.some (function(item,index,array) {2     return (item>2); 3 }); 4 Console.log (someresult);        // true

Merge Method :

ECMAScript adds two methods for merging arrays, which iterate over all the items in the algebraic group and then build a final return value.

Reduce (): Starting with the first item of the array, traversing through to the end, accepting two parameters, a function called on each item and an initial value as the base of the merge, where the function accepts 4 parameters, the previous value, the current value, the index of the item, and the array object. Any return value of this function will be automatically passed to the next item as the first argument.

1 var numbers = [1,2,3,4,5,4,3,2,1]23var Result = Numbers.reduce (  function(prev,cur,index,array) {4     return prev+cur; 5 }); 6 Console.log (Result);    //  -

Reduceright (): Same as reduce, except that the traverse begins in a different direction.

JavaScript Advanced Programming Learning Note 2

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.