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

JavaScript scope

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.


#In JavaScript, variables declared with var actually have scope.

If a variable is declared inside the function body, the scope of the variable is the entire function body, and the variable cannot be referenced outside the function body:

'use strict';function foo () {
var x = 1;
x = x + 1;
}

x = x + 2; // ReferenceError! Unable to reference variable x
# outside the function body

##If two different functions declare the same variable, the variable will only work within the respective function body. In other words, variables with the same name inside different functions are independent of each other and do not affect each other:

'use strict';function foo() { var x = 1;
x = x + 1;
}function bar() {
var x = 'A';
x = x + 'B';
}

##JavaScript Local scope

Variables are declared within the function, and the variables are local scope.

Local variables: can only be accessed inside the function.

// The carName variable cannot be called here

function myFunction() {
var carName = "Volvo";
// The carName variable can be called within the function
}

Note:

Because local variables only act within the function, 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.

Global scope

Variables not defined within any function have global scope. In fact, JavaScript has a global object window by default, and variables in the global scope are actually bound to a property of window:

'use strict';

var course = 'Learn JavaScript';

##alert(course); // 'Learn JavaScript'

alert(window. course); // 'Learn JavaScript'

Therefore, directly accessing the global variable course is exactly the same as accessing window.course.

<!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>

JavaScript variable life cycle

JavaScript variable life cycle is initialized when it is declared.

Local variables are destroyed after the function is executed.

Global variables are destroyed after the page is closed.

Function parameters

Function parameters only work within the function and are local variables.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var a =1; function test(){ alert(a); //a為undefined! 這個a并不是全局變量,這是因為在function scope里已經(jīng)聲明了(函數(shù)體倒數(shù)第4行)一個重名的局部變量, //所以全局變量a被覆蓋了,這說明了Javascript在執(zhí)行前會對整個腳本文件的定義部分做完整分析,所以在函數(shù)test()執(zhí)行前, //函數(shù)體中的變量a就被指向內(nèi)部的局部變量.而不是指向外部的全局變量. 但這時a只有聲明,還沒賦值,所以輸出undefined。 a=4 alert(a); //a為4,沒懸念了吧? 這里的a還是局部變量哦! var a; //局部變量a在這行聲明 alert(a); //a還是為4,這是因為之前已把4賦給a了 } test(); alert(a); //a為1,這里并不在function scope內(nèi),a的值為全局變量的值 </script> </head> <body> </body> </html>
submitReset Code