In-depth analysis of preparsing and side effects in JavaScriptvar
Side effects of var
There is a small difference between implicit global variables and explicitly defined global variables, which is the ability to leave variables undefined through the delete operator.
Global variables created through var (created in any program other than functions) cannot be deleted.
Implicit global variables created without var (regardless of whether they are created in a function) can be deleted.
This shows that, technically, implicit global variables are not really global variables, but they are properties of the global object. Properties can be deleted through the delete operator, but variables cannot:
// 定义三个全局变量 var global_var = 1; global_novar = 2; // 反面教材 (function () { global_fromfunc = 3; // 反面教材 }()); // 试图删除 delete global_var; // false delete global_novar; // true delete global_fromfunc; // true // 测试该删除 typeof global_var; // "number" typeof global_novar; // "undefined" typeof global_fromfunc; // "undefined"
In ES5 strict mode, undeclared variables (such as the two negative teaching materials in the previous code snippet) When working it throws an error.
Single var form
Using a single var statement at the top of a function is a more useful form. The benefit is:
Provides a single place to go Find all local variables needed for the function
Prevents logic errors where variables are used before they are defined
Helps you remember declared global variables, so there are fewer global variables //zxx:here I'm a little confused myself...
Less code (type, value, single line completion)
The single var form looks like this:
function func() { var a = 1, b = 2, sum = a + b, myobject = {}, i, j; // function body... }
You can use one var statement to declare multiple variables, separated by commas. It's a good idea to initialize variables and values at the same time like this. This prevents logic errors (the initial value of all uninitialized but declared variables is undefined) and increases the readability of the code. After you see the code, you can know the general purpose of these variables based on the initialized values, such as whether they are to be used as objects or as integers.
You can also do some actual work when declaring, such as sum = a + b in the previous code. Another example is when you use DOM (Document Object Model) references, you can Use a single var to specify DOM references together as local variables, as shown in the following code:
function updateElement() { var el = document.getElementById("result"), style = el.style; // 使用el和style干点其他什么事... }
vars variable preparsing
In JavaScript, you can Declaring multiple var statements anywhere in a function and having them function as if they were declared at the top of the function is called hoisting. Logic errors can occur when you use a variable and then redeclare it later in a function. For JavaScript, as long as your variable is in the same scope (same function), it is considered declared, even when it is used before the var declaration. Look at the following example:
// 反例 myname = "global"; // 全局变量 function func() { alert(myname); // "undefined" var myname = "local"; alert(myname); // "local" } func();
In this example, you may think that the first alert pops up "global", and the second alert pops up "loacl". This expectation is understandable, because at the time of the first alert, myname was not declared. At this time, the function must naturally look at the global variable myname, but this is not how it actually works. The first alert will pop up "undefined" because myname is treated as a local variable of the function (even though it is declared later), and all variable declarations are suspended to the top of the function. Therefore, to avoid this confusion, it is best to declare all the variables you want to use in advance.
The above code snippet may behave like the following:
myname = "global"; // global variable function func() { var myname; // 等同于 -> var myname = undefined; alert(myname); // "undefined" myname = "local"; alert(myname); // "local"} func();
For the sake of completeness, let’s mention something slightly more complicated at the execution level. Code processing is divided into two stages. The first stage is variable, function declaration, and normal format parameter creation. This is a stage of parsing and entering context. The second phase is code execution, where function expressions and unqualified identifiers (for declared variables) are created. However, for practical purposes, we will adopt the concept of "hoisting", which is not defined in the ECMAScript standard and is generally used to describe behavior.
Accessing the global object
In the browser, the global object can be accessed anywhere in the code through the window attribute (unless you do something outrageous, such as declaring a local variable named window). But in other contexts, this convenience property might be called something else (or even not available in the program). If you need to access the global object without a hard-coded window identifier, you can do the following in function scope at any level:
var global = (function () { return this; }());
This method can obtain the global object at any time Object, because it is called as a function in the function (not constructed through new), this always points to the global object. Actually this bug doesn't apply to ECMAScript 5 strict mode, so you have to take a different form when in strict mode. For example, if you are developing a JavaScript library, you can wrap your code in an immediate function, and then pass a reference to this from the global scope as a parameter of your immediate function.
The above is the detailed content of In-depth analysis of preparsing and side effects in JavaScriptvar. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

The reason for the error message indicates that in the python code, an object (represented by the self variable) is used, but the object does not have an attribute named k. This may be because the object does not have this property defined, or a type error in the code causes the object to not be of the expected type. How to Fix To resolve this error, you may need to do one or more of the following: Check your code for the error and make sure the object referenced by the self variable has a property named k. Check your code for type errors and make sure the object referenced by the self variable is of the expected type. If the attribute is missing, you need to define this attribute in the class and use tryexcept to get this error. If you are sure that k is an attribute that is not defined in the class, please confirm

llet, var, and const represent block scope variables, function scope variables, and constants respectively. Detailed introduction: 1. let, used to declare a variable in a block scope. A variable declared using let cannot be accessed before it is declared. This is the so-called temporary dead zone; 2. var, used to declare the key to a variable. Word, the declared variable is in function scope or global scope and is not restricted by block-level scope; 3. const, used to declare a constant. Once assigned, the variable cannot be reassigned. The value is after declaration. Cannot be modified etc.

Command introduction: Use the df-h command to view the total capacity and used capacity of each file system. Your output shows that /dev/mapper/centos-root takes up 47% of the space, which is 36G. Use the du-h-x–max-depth=1/ command to check the size of each subdirectory under the root directory. You can find out the directories that take up a lot of space based on the output, such as /var, /home, /usr, etc. You can run the command du-h-x--max-depth=1/var to check the size of each subdirectory in the /var directory. Continue this process until you find a file or directory that takes up more space. Recommended articles https://blog.csdn.n

To understand the different characteristics of var, let, and const, you need specific code examples. In JavaScript, there are many ways to declare variables, the most common of which include using the var, let, and const keywords. Although they are both used to declare variables, they have different characteristics regarding scope and mutability. The differences between them are explained below with specific code examples. var keyword Let’s first look at the usage of var keyword. It is the earliest introduced way to declare variables, with global scope and

Windows Subsystem for Android is here When Microsoft announced Windows 11 last year, it said Android apps would run on the new operating system. Unfortunately, it wasn't released in time and ended up heading to Insiders later this year. It will initially only have 50 apps available, curated by Microsoft and Amazon. Now anyone can use it if you meet the hardware requirements and it will offer over a thousand applications. I'm a big extension to the small subset of Insiders I've been able to test, but it's still a small part of the Amazon App Store. I did ask the developers when they would be able to submit the app and run it on Windows
