亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript scope

A collection of scope accessible variables.


JavaScript scope

In JavaScript, objects and functions are also variables.

In JavaScript, scope is a collection of accessible variables, objects, and functions.

JavaScript function scope: The scope is modified within the function.


JavaScript local scope

Variables are declared within a function and have a local scope.

Local variables: can only be accessed inside the function.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>局部變量在聲明的函數(shù)內(nèi)可以訪問。</p>
<p id="demo"></p>
<script>
    myFunction();
    document.getElementById("demo").innerHTML =
            "我可以顯示 " +  typeof carName;
    function myFunction()
    {
        var carName = "Volvo";
    }
</script>
</body>
</html>

Run the program and try it


Because local variables only act within the function, it is different Functions can use variables with the same name.

Local variables are created when the function starts executing, and will be automatically destroyed after the function is executed.


JavaScript global variables

A variable defined outside a function is a global variable.

Global variables have global scope: all scripts and functions in the web page can be used.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>全局變量在任何腳本和函數(shù)內(nèi)均可訪問。</p>
<p id="demo"></p>
<script>
    var carName = "Volvo";
    myFunction();
    function myFunction()
    {
        document.getElementById("demo").innerHTML =
                "我可以顯示 " + carName;
    }
</script>
</body>
</html>

Run the program and try it


If the variable is not declared within the function (var is not used keyword), this variable is a global variable.

In the following example, carName is within the function, but is a global variable.

Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>
<p>
如果你的變量沒有聲明,它將自動(dòng)成為全局變量:
</p>
<p id="demo"></p>
<script>
myFunction();
document.getElementById("demo").innerHTML =
"我可以顯示 " + carName;
function myFunction() 
{
    carName = "Volvo";
}
</script>
</body>
</html>

Run the program and try it


Global variables in HTML

In HTML, global variables are window objects: all data variables belong to the window object.

Example

##<!DOCTYPE html>


<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>
    在 HTML 中, 所有全局變量都會成為 window 變量。
</p>
<p id="demo"></p>
<script>
    myFunction();
    document.getElementById("demo").innerHTML =
            "我可以顯示 " + window.carName;
    function myFunction()
    {
        carName = "Volvo";
    }
</script>
</body>
</html>

Run the program to try it



Continuing Learning
||
<html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p> 在 HTML 中, 所有全局變量都會成為 window 變量。 </p> <p id="demo"></p> <script> myFunction(); document.getElementById("demo").innerHTML = "我可以顯示 " + window.carName; function myFunction() { carName = "Volvo"; } </script> </body> </html>
submitReset Code