Confusions 1: varobj1newObject (); varobj2obj1; obj1name & quot; Sunshine Xiaoqiang & quot; alert (obj2name); output result: five basic types in sunshine Xiaoqiang JavaScript: undefined, Null, Boolean, Number, and Str
Confusions 1:
Var obj1 = new Object (); var obj2 = obj1; obj1.name = "Sunshine Xiaoqiang"; alert (obj2.name); // output result: Sunshine Xiaoqiang
Five Basic Types in JavaScript: Undefined, Null, Boolean, Number, and String are accessed by value. You can perform operations to save the actual values in the variables. The memory space is as follows:
var num1 = 5;var num2 = num1;
The value of the reference type is an object stored in the memory. JavaScript does not allow access to the location in the memory. That is to say, the object's memory space cannot be operated directly. The value assignment operation of the reference type actually creates a new reference.
Confusions 2:
How does one recycle memory in JavaScript?
JavaScript has an automatic garbage collection mechanism. memory resources that are no longer used will be released by the system. The specific implementation in the browser usually has two policies: VcD48cD4xoaKx6rzHx + Wz/wlc + MrVvK + 3vcq9ysex6rzHx + Wz/agjpc9wpjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140513/20140513090654230.jpg" alt = "\"/>
When a variable enters the environment (for example, declaring a variable in a function), it is marked as "entering the environment". When the variable leaves the environment, it is marked as "out of the environment" and can be recycled at this time. The garbage collector regularly recycles resources.
2. Reference count:
Another uncommon garbage collection policy is reference count.
The trace records the number of times each value is referenced. When the number of times this value is applied becomes 0, it is released. Some objects in IE are not native JavaScript objects. The objects in BOM and DOM are implemented in the form of COM (Component Object Model) objects, while the garbage collection mechanism of COM objects adopts the reference counting policy, this will cause the issue of circular reference.
var element = document.getElementById("som_element");var myObject = new Object();myObject.element = element;element.someOject = myObject;To solve the above problem, IE9 converts both BOM and DOM objects into real JavaScript objects.
Confused 3:
Var colors = ["red", "blue", "green"]; colors. length = 2; alert (colors [2]); // output: undefined
The length attribute of the array is not read-only. You can set this attribute to remove items from the end of the array.
Confused 4:
var colors = ["red", "blue", "green"];alert(colors.toString()); //red,blue,greenalert(colors.valueOf());//red,blue,greenalert(colors);//red,blue,green
The alert () method receives a string. To create a string, the toString method of each item is called.
In addition, the toLocaleString () method often returns the same value as the toString () and valueOf () methods, but not always.
var person1 = {toLocaleString : function(){return "Nikolaos";},toString : function(){return "Nicholas";}};var person2 = {toLocaleString : function(){return "Grigorios";},toString : function(){return "Greg";}};var people = [person1, person2];alert(people);//Nicholas, Gregalert(people.toString()); //Nicholas, Gregalert(people.toLocaleString()) //Nikolaos, GrigoriosThe only difference between the preceding two methods is that the toLocaleString method of each item is called to obtain the value of each item, instead of the toString method.
The toLocaleString (), toString (), and valueOf methods inherited by the array are separated by commas by default. Different separator strings can be constructed using the join () method.
var colors = ["red", "green", "blue"];alert(colors.join(",")); //red,green,bluealert(colors.join("||")) //red||green||blueConfusions 5:
var colors = ["red", "blue"];colors.push("brown");colors[3] = "black";alert(colors.length); //4var item = colors.pop();alert(item); //"black"The ECMAScript array also provides a way to make the array behavior look similar to other data structures. Arrays can be used to press stacks and output stacks Like stacks.
var colors = new Array();var count = colors.push("red", "green");alert(count); //2count = colors.push("black");alert(count); //3var item = colors.pop();alert(item); //"black"alert(colors.length); //2The stack method and the array method can be used together.
The access rules of the queue data structure are also supported as follows:
var colors = new Array();var count = colors.push("red", "green");alert(count);count = colors.push("black");alert(count);var item = colors.shift();alert(item); //"red"alert(colors.length); //2Puzzles 6:
alert(sum(10, 10));function sum(num1, num2){return num1 + num2;}The above code can be executed, and the execution result is 20. Modify the above Code slightly, as shown below:
alert(sum(10, 10));var sum = function (num1, num2){return num1 + num2;}
Cannot be executed. Error: undefined is not a function
The parser will first read the function declaration and make it available (accessible) before executing any code. The second example above cannot be executed because the function is located in an initialization statement, instead of a function declaration.