JavaScript-reference transfer of object parameters

Source: Internet
Author: User
Tags javascript hoisting
1. JavaScripthoisting & gt; see the example. We use the Chrome console as the JS runtime environment. Directly execute console. log (a) above and throw the notdefined error without any suspense. This is expected. Let's look at the evolved code below: the variable is not defined before... Syntax 1. JavaScript hoisting> Please refer to the example. We use Chrome console as the JS runtime environment. The console. log (a) is directly executed above, and the not defined error is thrown without any suspense. This is expected. Let's look at the evolved code below: The variable has no defined errors. 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 plays a role in advance. In JavaScript, variables declared in subsequent statements can be used in advance. This feature is called Hoisting by a foreign user (ben cherry ). It can be understood that the declaration of variables is in advance, so no error will be reported if the variable is used before the declaration of variables. 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. Then write the function call at the beginning, but the definition of the function is written at the end.> Let's take a look at the problem that the output value of a is 'undefined. 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. This is critical. 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 final equivalent code should be like this: 2. you can call the method of string literal directly to call the method of string literal. The JS parser will automatically convert the string variable to a string type for temporary storage, and then call the method on the string, then, the temporary string type is destroyed. So you will see the following usage. We can understand that the parser declares a variable in the background to store this string and then calls the length method.
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.