JS--Scope problem

Source: Internet
Author: User

<title>js– scope problems</title> js– scope issues Table of Contents
    • Variable declarations and function declarations
function F () {    a = 3;                       equivalent to WINDOW.A = 3}f (); alert (a);                        3functionF() {    vara = 3;} f (); alert (a);                        Not defined
var t = 10; function F () {    3
var t = 10; function F () {    vartTen
var a = 3; function F1 () {    a = 4;    function F2 () {        a = 5;    }    F2 ();} F1 ();d ocument.write (a);       5
var a = 3; function F1 () {    vara = 4;    function F2 () {        a = 5;    }    F2 ();} F1 ();d ocument.write (a);       3
Variable declarations and function declarations

For the parsing process of the declaration: Parameter –> variable –> function declaration

Rule 1:
For variable declarations
If there are arguments and declared variable names that are the same, they cannot be overwritten (no re-declaration of a variable), but there are assignments that override, and rules 1 and 2 only discuss variable declarations.

Rule 2:
for function declarations
Function declarations can override variable declarations and parameters

--For rule 1 function F (a) {    vara;                       Alert (a) is not declared here    ;                    8}f (8); function F (a) {    vara = 2;    alert (a);                    2}f (3);
// --For rule 2function F() {var a; alert (a);function a() {} alert (a);} f ();// outputs two a functions, stating that the original a declaration is overwritten by a function declarationfunction F() {var a=' Hello ';// At the end of the assignment, function A is overwritten with ' hello ', and the assignment is parsed after the variable declaration and function declaration analysis.alert (a);function a() {} alert (a);} f ();// output of two ' Hello 'function a(b) {alert (b);// First, assign a value of 1b =function() {// there is no function declaration, direct assignment, and then assign a value to a functionalert (b); } b ();} A (1);// The result is 1 and a function

JS--Scope problem

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.