JavaScript  Scope

Scope — Collection of accessible variables .

Global variables or functions can override window Object variables or functions ;

Local variables and window Objects can override global variables and functions .


JavaScript Scope

stay JavaScript in , Objects and functions are also variables .

stay JavaScript in , The scope is accessible variables , object , The set of functions .

JavaScript Function scope : The scope is modified in the function .

notes : Because local variables only act in functions , So different functions can use variables with the same name .

Local variables are created at the beginning of function execution , After the function is executed, local variables will be destroyed automatically .


JavaScript Global variables

Variables are defined outside functions , It's a global variable .

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

notes : If a variable is not declared within a function ( Not used var keyword ), This variable is a global variable .

In the following example carName Within the function , But for global variables .

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body> <p> Global variables are accessible in any script or function .</p>
<p id="demo"></p>
<script>
var carName = "Volvo";
myFunction();
function myFunction()
{
document.getElementById("demo").innerHTML =
" I can show " + carName;
}
</script> </body>
</html>

JavaScript Variable life cycle

JavaScript The variable life cycle is initialized when it is declared .

Local variables are destroyed after function execution .

Global variables are destroyed when the page is closed .


Function parameter

Function parameters only work in functions , It's a local variable .


HTML Global variable in

stay HTML in , The global variable is window object : All data variables belong to window object .

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body> <p>
stay HTML in , All global variables will become window Variable .
</p>
<p id="demo"></p>
<script>
myFunction();
document.getElementById("demo").innerHTML =
" I can show " + window.carName;
function myFunction()
{
carName = "Volvo";
}
</script> </body>
</html>

JavaScript The main points of ( One ) Variable - More articles on scope

  1. javascript Variable scope and variable promotion in

    stay javascript in , It's necessary to understand the scope and promotion of variables . Does this look simple , But it's not what you think , There are also some important details that you need to understand . Variable scope “ The scope of a variable indicates the existence of the variable ...

  2. JavaScript Basics —— Understand variable scope

    Once you start in JavaScript Add conditions to the application . Functions and loops , You need to understand the scope of variables . Variable scope specifies how to determine the value of a specific variable name on the line of code being executed . JavaScript Allows you to define both global version and Bureau ...

  3. javascript Variable scope and variable promotion are introduced in detail

    stay javascript in , It's necessary to understand the scope and promotion of variables . Does this look simple , But it's not what you think , There are also some important details that you need to understand the scope of variables “ The scope of a variable represents the context in which the variable exists ...

  4. 【JavaScript Starting from scratch 】 Variable scope

    Variable scope The scope of a variable (scope) Is the area in the program source code where the variable is defined . Global variables have global scope , stay JavaScript Everywhere in the code is defined . However, variables declared within a function are only defined within the function itself . He ...

  5. JavaScript Learning Series 2 One JavaScript Variable scope in

    Before writing this article , Remind me again JavaScript It's case sensitive language // 'test', 'Test', 'TeSt' , 'TEST' yes 4 Two different variable names JavaScript The variables in the , above all ...

  6. javascript Variable scope in

    I saw one on the Internet js Interview questions <script type="text/javascript"> var tt = 'aa'; function test() { alert( ...

  7. javascript One of the pain points is the variable scope

    1. use var Declared variables are scoped , For example, we use... In functions var Declare a variable 1 'use strict'; 2 function num(){ 3 // use var Declare a variable num1 4 var num1 ...

  8. 【Javascript series 】 Variable scope

    Problem description This article focuses on javascript Variables and their scopes . 1   Content area stay js in , Variables can be roughly divided into global variables ( Global scope ) And local variables ( Local scope ): With keywords var Defining variables ( Global variables , It can be omitted va ...

  9. javascript About closure variable scope

    In the project from time to time will encounter some small problems and solutions : 1 The child function calls the variable in the parent function : Add return: var a=1; function num(){ var b=2; return b; } num()+ ...

Random recommendation

  1. java ---- Interview questions

    1.java How to deal with language exceptions , keyword :throws.throw.try.catch.finally What is the meaning of each of them ?finally The code is in return After or before ? throws It's getting exceptions , ...

  2. php yii Frame usage MongoDb

    1. install function php composer.phar require --prefer-dist yiisoft/yii2-mongodb or add "yiisoft/yii2-mongo ...

  3. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Ideas : The result of traversing palindrome structure from back to front is the same as that of traversing palindrome structure from front to back , You can use the structure of a stack ...

  4. How to optimize Java performance ?

    about Java The students who are more concerned about the performance probably know that <Java Performance> This book , generally speaking , Many students write in their daily life Java Code I don't care much about performance when I'm working , But before we write Code Of ...

  5. Tangled CLI C++ And Native C++ Interaction

    I've been writing something recently , It's about CLR C++ And Native C++ The problem of calling each other , result ........... Tangled up . Interaction prototypes The interaction prototype is like this : void* avio_alloc_context( un ...

  6. php character string

    <?php /* * String output * echo() Output multiple or multiple strings * print() Output string * printf() Format output string * String interception * substr() To refer to a character ...

  7. vue The page is fixed and locked

  8. spring Configure scheduled tasks Scheduled

    One : stay spring Configured xml File to add 3 Article namespace xmlns:task="http://www.springframework.org/schema/task" xsi:sche ...

  9. Use Hystrix Carry out microservice degradation management

    Preface : At present, our project is microservice architecture , be based on dubbo frame , Calls between services are made through rpc Called . There was no problem at first , Healthy operation of the project . good . But after a while , There are always people on the line who fail to query the order , It took a while to find out . this ...

  10. PCIe Debugging experience _DMA part1

    author :East  FPGA That thing 1.PCIe Of DMA Introduced in PCIe Required in DMA Project , Be sure to see first XAPP1052, It contains a DMA The reference design of , It's very helpful for beginners . XAPP1052 Contained in the ...