


Authoritative Guide to JavaScript Study Notes Sharing Variable Scope_Javascript Skills
I don’t know how you understand the “declaration” and “definition” of variables in the language.
My understanding is as follows:
“Declaring” a variable means just declaring it, while “defining” a variable means declaring it. , and assigned a value.
For example:
var name;//Just a statement
var num = 11;//Declaration and assignment, that is, it is defined
var password = "yangjiang";//Declaration and assignment, that is, it is defined
The following are a few Point summary:
Scope of variables: global and local. (Note: If you try to read the value of an undeclared variable, JavaScript will generate an error)
First point: In the case of using the var keyword to modify variables, if a local variable or function parameter is declared The name is the same as the name of a global variable,
then the global variable is effectively hidden.
For example:
var scope1 = "global"; //var modification
function checksScope(){
var scope1 = "local";//var modification
document.write(scope1);
}checksScope();//local
Second point: If you try to give a variable declared without the var keyword, then the implicitly declared variable is always created as a global variable, even if
the variable is only used within a function body (It will only take effect if the function is run.) Note that function nesting is not supported.
For example:
scope2 = "globalAAAAAA";/ / No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA"; // No var modification is used (js will declare it as a global variable by default)
document.write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write ("," myscope);
}
checkScopeA();//localAAAAA, myLocalAAAA *A
document.write("
" scope2);//localAAAAA *B
document.write("
" myscope);//myLocalAAAAA *C
If you comment out the code at *A in the above example,
For example:
scope2 = "globalAAAAA";//No var modification is used (js will declare it as a global variable by default)
function checkScopeA(){
scope2 = "localAAAAA";//No var modification is used (js will declare it as a global variable by default)
document. write("
" scope2);
myscope = "myLocalAAAAA";//No var modification is used (js will declare it as a global variable by default)
document.write("," myscope );
}
//checkScopeA(); *A
document.write("
" scope2);//globalAAAAA *B
document.write("" myscope);//An error occurred *C
Because the function checkScopeA is not executed, the output at *B is globalAAAAA;
Because the function checkScopeA is not executed, the variable myscope is not Declaration, if you try to read an undeclared variable, an error will occur.
The third point:
In JavaScript, function definitions can be nested. Since each function has its own local scope, it is possible to have several nested levels of local scopes.
For example:
var scope3 = "global scope" ; //A global variable is defined
function checkScopeB(){
var scope3 = "local scope"; //A local variable is defined, overriding the global variable scope3
function nested(){
var scope3 = "nested scope"; //A local variable is defined inside the function of the function
document.write("
" scope3); //nested scope
}
nested();
}
checkScopeB();//nested scope
The fourth point:
In javascript, there is no block-level scope, it is declared in the function All variables, no matter where they are declared, are declared throughout the function.
In JavaScript, there is no block-level scope. All variables defined in a function, no matter where they are defined, are defined throughout the function.
For example:
function test(o){//According to the above description: the scopes of the three variables i, j, and k in this function are the same.
var i = 0; //Variable i is defined throughout the function
if(typeof o == "object"){
var j = 0; //Variable j is defined throughout the function Defined, not just in the if statement block
for(var k=0;kdocument.write("
The value of k is: " k);
}
document.write("
The value of k outside the for loop: " k); //K at this time is still defined, k=10
}
document.write("
value of j:" j); //Variable j has been declared, but it may It has not been initialized because the parameters passed into the function may not be objects, and the if statement block will not be executed
}
This function is called in two ways:
Method 1: Pass Input object
test({});//Output result: Comment in the above example
Method 2: Pass nothing
test();//Output result: value of j: undefined
What I don’t understand is why the output result in the second method is undefined. What I guessed at the time was: The value of j: 0
Later, this book said:
Since local variables are declared (or defined) in the entire function body, this means that in the entire function body The global
variable with the same name is hidden in . Although local variables are declared (or defined) throughout the function body, they will not be initialized before the var statement is executed.
In this case, the output result of the call in the second method above is easier to explain. Since the variable j is defined in the entire function, and since the parameters passed into the function are empty, the if statement in the function body will not be executed, thus making the value of j undefined. (This is my understanding based on the sentence in the book above)
The following example is a better explanation:
var sssss = "Global variable";
function f(){
document.write ("
" sssss);//Output: undefined instead of outputting "global variables"
var sssss = "local variables";
document.write("
" sssss);//Output: local variable
}

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
