javascript shows the difference between global variables and implicit global variables

  • 2021-07-18 06:55:37
  • OfStack

In JavaScript, global variables are declared in two ways

Use var to display declared global variables

Implicit global variables that do not use var declarations

The difference between the two is whether it can be deleted by delete operator

Look at 1 piece of code first


var a = 'a'; //  Explicitly declared global variables 
b = 'b'; //  Implicitly declared global variables 
 
console.log(a); // a
console.log(b); // b
console.log(window.a); // a
console.log(window.b); // b

In js, global variables are actually attributes of global objects (window), so global variables declared in both ways can be obtained through window

Try to delete with delete


//  Explicitly declared global variables cannot be deleted 
delete a; //  Return  false 
 
//  Implicitly declared global variables can be deleted 
delete b; //  Return  true 
 
//  Deletion situation 
console.log(typeof a); // string
console.log(typeof b); // undefined

The delete operator can delete a property of an object, but if the property is a non-configurable (non-configurable) property, false is returned on deletion (an exception is thrown in strict mode)

This means that variables declared with var are not configurable, and getOwnPropertyDescriptor is used to get objects describing property characteristics to verify this point


Object.getOwnPropertyDescriptor(window, a); // {value: "a", writable: true, enumerable: true, configurable: false}
Object.getOwnPropertyDescriptor(window, b); // {value: "b", writable: true, enumerable: true, configurable: true}

The fundamental difference between the two is that explicitly declared variables are not configurable and cannot be deleted by the delete operator

It should be noted that if configurable value 1 denier is false, the object describing the attribute characteristics cannot be modified, so the global variables declared by display cannot be deleted by delete by modifying the attribute descriptor, but conversely, the global variables declared implicitly cannot be deleted by delete


b = 'b';
var descriptor = Object.getOwnPropertyDescriptor(window, b);
descriptor.configurable = false;
Object.defineProperty(window, b, descriptor);
delete b; //  Return  false 

The following are the additions of other netizens

Global Variables and Implicit Global Variables in JavaScript

One small difference between implicit and explicitly defined global variables is the ability to leave variables undefined through the delete operator.

1. Global variables created through var (created in programs other than functions) cannot be deleted.
2. Implicit global variables created without var (regardless of whether they are created in functions) can be deleted.

This shows that implicit global variables are not really global variables technically, but they are properties of global objects. Attributes can be deleted by delete operator, but variables cannot:


//  Definition 3 Global variables 
var global_var = 1;
global_novar = 2; //  Negative teaching material 
(function () {
  global_fromfunc = 3; //  Negative teaching material 
}());
 
//  Attempt to delete 
delete global_var; // false
delete global_novar; // true
delete global_fromfunc; // true
 
//  Test the deletion 
typeof global_var; // "number"
typeof global_novar; // "undefined"
typeof global_fromfunc; // "undefined"

In the browser, global objects can be accessed from anywhere in the code through the window property (unless you do something rather outrageous, such as declaring a local variable named window). But in other contexts, this handy property might be called something else (even unavailable in the program). If you need to access global objects without hard-coded window identifiers, you can do the following at any level of function scope:


var global = (function () {
  return this;
}());

This method can get the global object at any time, because it is called as a function in the function (not constructed by new), and this always points to the global object. In fact, the disease does not apply to ECMAScript 5 Strict Mode, so when in Strict Mode, you have to take a different form. For example, if you are developing an JavaScript library, you can wrap your code in an instant function, and then pass a reference to this as an argument to your instant function from the global scope.

The above is the difference between javascript showing global variables and implicit global variables. The fundamental difference between the two is that explicitly declared variables are not configurable and cannot be deleted through delete operators. I hope everyone will pay more attention to other articles on this site.


Related articles: