JavaScript variables/Functions lifting __ function

Source: Internet
Author: User
Tags anonymous javascript hoisting

1.JavaScript hoisting (variable/function elevation)


The following experiments are done through the Firefox console. Console.log (a); REFERENCEERROR:A is not defined

Console.log (a);
However, the results will be different if you add the definition of the variable later.
Console.log (a); var a = 10; Undefined

The error that was not defined in the previous variable is gone and instead tells us that the value of a is ' undefined '.

Regardless of why A's value is ' undefined ', you can at least know that the variable is defined, because the error of the previously reported ' a being not defined ' has not been made.
This is precisely because of the role of a declarative elevation feature in JavaScript.

In JavaScript, variables declared in the following statements can be used in advance, a feature called hoisting (unofficial term) by a foreign Netizen (Ben Cherry).

I see in the <<javascript mode >> book that the word is translated as "ascension", referring to the variable declaration in advance. You can understand that the declaration of a variable is advanced, so using a variable before a variable declaration does not report an error. And this feature is not limited to variable names, but also to the declaration of functions.
The first call to the function foo () also reports ' not defined ' errors. It is reasonable and legitimate at the same time. Because there is no such thing called Foo () from beginning to end.
Referenceerror:foo is not defined

Foo ()

The function call is then written at the front, but the definition of the function is written after.

Foo (); function foo () {console.log ("I am foo");} I am Foo
found that the normal output of the content we want.

there needs to be a deep understanding of the hoisting feature. It's ahead of time, but the assignment to the variable is not followed in advance.

Which is why we can use variable a in the first sentence but its value is ' undefined '. A variable that is declared in JavaScript but has not yet been assigned the value defaults to ' undefined '.
According to hoisting, the resulting equivalent code is almost always the same:

var A;

Console.log (a);

A = 1;
1. Anonymous functions cannot be invoked before the declaration

Syntaxerror:missing (Before formal parameters

Foo1 ();
var foo1 = function{
  console.log ("I am Foo1");
}
As the hoisting attribute described above, a function can be defined before the call can also be defined after the call:

But this is limited to functions defined in this way. The above rules for anonymous functions do not apply.

2. Parameter changes affect external function



When a parameter passed to a function is an array or an object, changes to the incoming parameter in the body of the function affect the original incoming value outside the body of the function.

In general, changes to parameters do not affect the value of the original variable, and the change only works in the function body, mainly because JavaScript variable passes are passed by value. Copies the value of the variable and passes it to the function's internal processing. But it is different for objects and arrays.
When the incoming argument is an array, an object, changes to the parameters in the body of the function are reflected on the original variable.

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" is changed by the
	                    function)	
	console.log (x);
	Console.log (y);


As you can see, the Mycar object's make attribute has been modified in the above code.

When you assign a value to an incoming array or object in the body of a function, the change is not reflected in the original variable outside the body of the function . 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);



The theory of the original variable is reflected in the changes within the function above. But the results are a bit hard to accept.
Really, my heart really "after" ah.

The reason is that when an assignment operation is used in a function body, the system creates a variable named Theobject. This theobject is a variable within the function, which is assigned to it only in function, outside of the Mycar or the original mycar.

This step differs from the original code simply by assigning a new value to the parameter in the body of the function or by changing the attributes of the parameter or the elements of the array.

The above comes from MDN's article on JavaScript. The link address is:

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Best wishes.

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.