1. JavaScript hoisting (variable/function upgrade)
The following experiments are conducted on the firefox Console.
Console. log (a); ReferenceError: a is not defined
However, if a variable is defined later, the result will be different.
Console. log (a); var a = 10; undefined
The error that has not been defined in the variable is gone. Instead, it tells us that the value of a is 'undefined '.
No matter why the value of a is 'undefined', at least we can know that the variable a is defined, because the previous error of 'a is not defined' is missing.
This is precisely because a declaration in JavaScript promotes the feature.
In JavaScript, variables declared in subsequent statements can be used in advance. This feature is called Hoisting by a foreign user (ben cherry ).
On the < > The word is translated as "promoted" in the book, which indicates that the variable declaration is made in advance. It can be understood that the variable declaration is made in advance, so no error will be reported if the variable is used before the variable declaration. In addition, this feature is not limited to variable names, but also has the same effect on function declaration.
The first call to function foo () also reports the 'not defined' error. This is both reasonable and legal. Because there is no definition of such a thing as foo () from start to end.
ReferenceError: foo is not defined
Then write the function call at the beginning, but the definition of the function is written at the end.
Foo (); function foo () {console. log ("I am foo");} I am foo
We found that the content we wanted was output normally.
The Hoisting feature needs to be thoroughly understood here. It only advances the declaration in advance, but the assignment of variables is not followed in advance.
That is why we can use variable a In the first sentence, but its value is 'undefined '. The default value of a variable declared in JavaScript but not assigned a value is 'undefined '.
According to Hoisting, the generated equivalent code is similar to this:
Var;
Console. log ();
A = 1;
1. Anonymous functions cannot be called before Declaration
SyntaxError: missing (before formal parameters
|
foo1();var foo1 = function{ console.log("I am foo1");} |
As the Hoisting feature described above, a function can be defined either before or after a call:
But only functions defined in this way. These rules are not applicable to anonymous functions.
2. parameter changes affect function exterior
When the parameter passed to the function is an array or object, changes to the passed parameter in the function body will affect the original incoming value of the function in vitro.
Generally, changes to parameters do not affect the value of the original variable. changes only take effect in the function body. The main reason is that javascript variables are passed by value. copy the value of the variable and pass it into the function for internal processing. however, objects and arrays are different.
When the input parameters are arrays and objects, changes made to the parameters in the function body are reflected in the original variables.
function myFunc(theObject) { theObject.make = "Toyota";}var mycar = {make: "Honda", model: "Accord", year: 1998}, x, y;x = mycar.make; // x gets the value "Honda"myFunc(mycar);y = mycar.make; // y gets the value "Toyota" // (the make property was changed by the function)console.log(x );console.log(y);
We can see that the make attribute of the mycar object has been modified in the above Code.
When the input array or object is assigned a value in the function body, this change is not reflected in the original variable in the function body.
The following is an example of an object:
function myFunc1(theObject) { theObject = {make: "Ford", model: "Focus", year: 2006};}var mycar = {make: "Honda", model: "Accord", year: 1998}, x, y;x = mycar.make; // x gets the value "Honda"myFunc1(mycar);y = mycar.make; // y still gets the value "Honda"console.log(x);console.log(y);
According to the internal changes of the above function, the original variable theory is reflected. However, the results are somewhat unacceptable.
Really, my heart is really "one after another.
The reason is that when the value assignment operation is used in the function body, the system creates a variable named theObject. This theObject is a variable in the function. Of course, assigning values to it only takes effect in the function body. The external myCar is still the original myCar.
The difference between this step and the operation of the original code lies only in whether to add a new value to the parameter in the function body or change the attribute of the parameter or the element of the array.
The above content is from the article about javascript in MDN. The link address is:
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
Best wishes.