JavaScript Advanced Programming (second Edition) learning (3)

Source: Internet
Author: User

Learn the contents of chapters fourth and fifth

Garbage collection

JavaScript has an automatic garbage collection mechanism, local variables exist only during function execution, and in this process, local variables are allocated space on the stack (or heap) memory to store their values. When the function is executed, the local variables do not exist, so they can release their memory for future use. There are two main types of garbage collection strategies:

Tag cleanup: Most commonly used in JavaScript. The garbage collector adds tags to all variables stored in memory at run time, and then it removes variables from the environment and tags of variables referenced by other variables in the environment. Variables that are tagged after this will be considered to be deleted. Finally, the garbage collector completes the memory cleanup work, destroys those tagged values, and reclaims their memory.

Reference count: Keeps track of the number of times each value is referenced. When a variable is declared and a reference type value is assigned to the variable, the value is referred to as 1, and if the same value is given another, the number of times plus 1. Conversely, if the variable that contains the value reference is given another value, the number of references is reduced by 1. When this value is 0, it means that there is no way to access the value again, so you can reclaim its memory. (However, when a circular reference is present, the reference count cannot be garbage collected.)

In addition, because objects in IE are not native JavaScript objects, such as objects in the BOM and DOM, are implemented in the form of COM (Component Object model) objects in C + +, and COM is a reference count

var element=document.getelementbyid (' some_element '); var myobject=New  Object (); // The following creates a circular reference myobject.element=element;element.someobject=MyObject; // to avoid the inability to reclaim memory because of circular references, manually clear myobject.element= null when using them ; element.someobject=  Null;

About optimizing memory because, for security reasons, it does not allocate too much memory to the browser, so memory is valuable. The best way to optimize memory is to execute the code to save the necessary data. Once the data is no longer useful, it is best to release its reference by setting it to null, which is called dereferencing. Properties that apply to most global variables and global objects.

function Createperson (name) {    var locationperson=New  Object ();    Locationperson=name;     return Locationperson;} var globalperson=createperson (' Mike '); // manually lifted globalperson=null;

Several methods in array

Stack method: After-in first-out, the use of the push () and the Pop () method, the former is added to the end of the array, and the latter is the end of the array to remove the item.

Queue method: FIFO, using push () and shift (), where shift is able to move the first item in the array and return the item, with the length minus 1.

Reorder methods: Reverse () and sort (), which are inverted arrays, while the latter is the default in ascending order of arrays. Sort () sometimes does not get the result we want, so it accepts a comparison function, the comparison function has two parameters, if the parameter 1 should precede the parameter 2 is a negative number, if it should be followed by a positive number, flat is 0;

function Compare (obj1,obj2) {    var val1=Obj1[propertyname];     var val2=Obj2[propertyname];     if (val1<val2) {        return -1;    } Else if (val1>val2) {        return 1;    } Else {        return 0;    }} Arr.sort (compare);

For arrays There is a powerful method of splice (), which has three types of usage:

Delete: only need to make 2 parameters, the first parameter is the location, the second parameter is the number of deletions; splice (2,1)

Insert: Accept 3 parameters, the first 2 parameters of the same meaning, only need to set the second parameter to 0, the third parameter is the item to be inserted, you can pass in four, five, any. such as splice ("2" "0" "Blue" "Red")

Replace: The second parameter sets the number of substitutions, and then the third parameter is the same as splice ("3" "1" "Blue" "Red");

5 iterations of the array: Every, filter ForEach map Some; the functions passed in these methods all accept three parameters: the value of the array item, the position of the item in the array, and the group object itself. Where every and some are very similar, the difference is that some returns true if one of the items is true, and every is to be true for each. Map an array of methods that run the specified function, returning a list of the results of the call. Filter returns a new array that consists of a true entry. None of these methods will change the original array.

JavaScript Advanced Programming (second Edition) learning (3)

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.