PS: This content belongs to the basic knowledge of JavaScript, suitable for beginners or the basic knowledge is not strong
/**
* The parameters of all functions in ECMAScript are "passed by value", that is, the values outside the function are copied to the parameters inside the function, and the value is copied from one variable to 21 variables.
* It is important to note that access variables are both "by value" and "by reference", while parameters can only be passed by value.
* PS1: When passing a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, or, in the concept of ECMAScript, an element in the arguments object).
* When passing values of reference types to a parameter,
* The address of this value in memory is copied to a local variable, because the change of this local variable is reflected outside of the function.
* **/
function Addten (num) {
Num +=10;
return num;
}
var count = 20;
var result = Addten (count);
Console.log (count); 20
Console.log (result); 30
//
function SetName (obj) {
Obj.name = "Andy";
}
var person = new Object ();
SetName (person);
Console.log (Person.name); Andy
Parsing: An object is created in the above code and will be saved in the variable person, and then this variable is passed to the SetName () function and then copied to obj. Inside this function, obj and person refer to the same object.
In other words, even if the variable is passed by value, obj accesses the same object by reference.
Many people mistakenly think that the objects modified in the local scope are reflected in the global scope, stating that the parameters are passed by reference. To prove that the object is passed by value, let's look at the following example:
function setName2 (obj) {
Obj.name = ' Andy ';
obj = new Object ();
Obj.name = ' Greg ';
}
var person2 = new Object ();
SetName2 (Person2);
Console.log (person2.name);//Andy
/**
* If person is passed by reference, the person is automatically modified to point to a new object whose Name property has a value of "Greg".
* However, when you visit Person.name again, the value displayed is still "Andy". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged.
* In fact, when you re-obj inside the function again, the variable refers to a local object. This local object is destroyed immediately after the function is executed;
* **/
/**
* Execution Environment and scope
* Each function has its own execution environment. When the execution flow enters a function, the environment of the function is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.
* The execution flow in the ECMAScript program is formally controlled by this convenient mechanism.
*
* **/
/**
* Manage Memory:
* Ensure that the least amount of memory is used for better performance of the page. The best way to optimize memory consumption is to save only the necessary data for the code in execution. Once the data is no longer useful, it is best to release its reference by setting its value to null-this is called dereferencing.
* This practice applies to most global variables and properties of global objects. Local variables are dereferenced when they leave the execution environment.
* However, releasing a reference to a value does not mean that the memory consumed by the value is automatically reclaimed. The real effect of dereferencing is to leave the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs.
* **/
function Createperson (name) {
var Localperson = new Object ();
Localperson.name = name;
Console.log (Localperson.name); Andy
return Localperson;
}
var Globalperson = Createperson (' Andy ');
Globalperson = null;
/**
* Reference type:
* A value (object) of a reference type is an instance of a reference type. In Ecmasript, a reference type is a data structure used to organize data and functionality together. It is also often referred to as a "class". But that's not a proper call.
* 1,object Type
* There are two ways to create an object instance. The first is the use of the new operator followed by the object constructor, such as:
* var person = new Object ();
* Person.name = "Andy";
* Person.age = ' 27 ';
* Another way is to use object literals notation
* var person ={
* Name: "Andy",
* age:27
* }
* In general, object properties are accessed using dot notation, which is a common syntax in many object-oriented languages. However, you can also use square bracket notation to access the properties of an object in JS. When you use square brackets syntax, you should place the property you want to access as a string in the
* in square brackets. Such as
* Alert (person["name"]); >>>> Alert (person.name)
* Functionally, there is no difference between the two methods of accessing object properties, but the main advantage of square brackets syntax is that you can access properties through variables such as:
* var proptrtyname = ' name ';
* Alert (person[proptrtyname]);
* If the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word, you can also use the square bracket notation:
* person["First name"] = "Andy"; Because the "First name" contains a space, it cannot be accessed by dot notation.
* **/
/**
* 2,array Type
* There are two basic ways to create an array.
* The first is the use of the array constructor,
* var color = new Array ();
* The second type is to use the array literal notation.
* var color = ["Red", "Blue", "green"];
*
* The length property of an array is very characteristic-it is not read-only. Therefore, by setting this property, you can remove an item from the end of the array or add a new item to the array
* **/
var colors = ["Red", "Blue", "green"];
Colors.length = 2;
Console.log (colors[2]); Undefined
If you set its Length property to a value that is greater than the number of array items, each new item will get a value of undefined
var colors2=["Red", "Blue", "green"];
Colors2.length = 4;
Console.log (Colors2[3]); Undefined
Therefore, it is also convenient to add new items at the end of the array by using the length property;
/**
* Array Detection
* If (value instanceof Array) {
*//To do ...
* }
* However, if the page contains more than one frame, there are actually more than two different global execution environments, and there are more than two different versions of the array constructors. If you pass an array to another frame from one frame, the incoming array is associated with the second
* The arrays that are created natively in the framework have their own different constructors.
* In order to solve this problem, ECMAScript5 added the Array.isarray () method;
*
* IF (Array.isarray (value)) {The browser that supports the Array.isarray () method has ie9+,.....
*//To do ...
* }
* If you want to resolve browser compatibility issues, because calling the object native ToString () method on any value, returns a string in the format "Object Nativeconstrucotrname", with each class having a [[Class] property inside it.
* The constructor name in the above string is specified in this attribute.
* Because the constructor name of the native array is independent of the global scope, using ToString () guarantees that a consistent value is returned. Using this, we solve the problem of compatibility of the browser to the detection array;
* **/
function IsArray (value) {
return Object.prototype.toString.call (value) = = ' [Object Array] ';
}
Similarly, you can test whether a value is a native function or a regular expression based on this idea:
function Isfunction (value) {
return Object.prototype.toString.call (value) = = ' [Object Function] ';
}
function Isregexp (value) {
return Object.prototype.toString.call (value) = = ' [Object RegExp] ';
}
/**
* Conversion Method:
* In fact, all objects have the tolocalestring ()/tostring ()/valueof () method, where the ToString () method of the call array returns a comma-delimited string that is stitched together as a string of each value in the array. and call
* VALUEOF () returns an array.
* **/
var colors3 = ["Red", "Blue", "green"];
Console.log (Colors3.tostring ()); Red,blue,green
Console.log (Colors3.valueof ()); ["Red", "Blue", "green"]
Console.log (COLORS3); ["Red", "Blue", "green"]
Console.log (COLORS3) receives the string parameter, so it calls the ToString () method in the background, resulting in the same result as calling the ToString () method directly;
/**
* Stack method: (last in First out)
* The ECMAScript array also provides a way for the array to behave like other data structures. Specifically, an array can behave like a stack, which is a data structure that restricts the insertion and deletion of items.
* The ECMAScript provides the push () and Pop () methods specifically for implementing a stack-like behavior.
* PUSH (): Add them one by one to the end of the array and return the length of the modified array;
* POP (): Method removes the last item from the end of the array and then returns the removed item;
* **/
/**
* Queue method: (FIFO)
* The queue adds items at the end of the list, removing items from the front end of the list.
* SHIFT (): It can move the first item in the array and return the item, minus 1 of the length
* Unshift ():
* **/
var colors4 =new Array ();
var count4= colors4.push (' Red ', ' green ');
Console.log (COUNT4); 2
Count4 = Colors4.push (' black ');
Console.log (COUNT4); 3
var item = Colors4.shift ();
Console.log (item); ' Red '
Console.log (colors4.length); 2
var colors5 =new Array ();
var count5 = colors5.unshift (' Red ', ' green ');
Console.log (COUNT5)
COUNT5 = Colors5.unshift (' black ')
Console.log (COLORS5)
Function-related knowledge points in JavaScript