Differences between global variables and implicit global variables displayed in javascript

Source: Internet
Author: User
This article describes the differences between global variables and implicit global variables in javascript. For more information, see the following article, for more information, see

In JavaScript, global variables can be declared in two ways.

  • Use var to display declared global variables

  • Implicit global variables declared without var

The difference between the two lies in whether they can be deleted through the delete operator.

First read a piece of code

Var a = 'a'; // explicitly declared global variable B = 'B'; // implicitly declared global variable console. log (a); // aconsole. log (B); // bconsole. log (window. a); // aconsole. log (window. b); // B

In js, global variables are actually attributes of the global Object (window). Therefore, 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 // Delete status console. log (typeof a); // stringconsole. log (typeof B); // undefined

The delete operator can delete an object attribute. However, if the attribute is a non-configurable attribute, false is returned during deletion (an exception is thrown in strict mode)

This means that the variables declared using var are not configurable. You can use getOwnPropertyDescriptor to obtain the object that describes the property features to verify this.

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 lies in that explicitly declared variables cannot be configured and cannot be deleted through the delete operator.

It should be noted that once the retriable value is false, the object describing the attribute characteristics cannot be modified. Therefore, you cannot modify the attribute descriptor so that the declared global variables can be deleted by delete, but in turn, this prevents implicitly declared global variables from being deleted by delete.

B = 'B'; var descriptor = Object. getOwnPropertyDescriptor (window, B); descriptor. retriable = false; Object. defineProperty (window, B, descriptor); delete B; // return false

The following is a supplement from other netizens:

Global variables and implicit global variables in JavaScript

There is a small difference between implicit global variables and clearly defined global variables, that is, the ability to define variables 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 (ignore whether or not they are created in the function) can be deleted.

This indicates that, technically, implicit global variables are not real global variables, but they are attributes of global objects. Attributes can be deleted using the delete operator, but variables cannot:

// Define three global variables var global_var = 1; global_novar = 2; // The opposite textbook (function () {global_fromfunc = 3; // The opposite textbook }()); // try to delete global_var; // falsedelete global_novar; // truedelete global_fromfunc; // true // test whether to delete typeof global_var; // "number" typeof global_novar; // "undefined" typeof global_fromfunc; // "undefined"

In the browser, global objects can be accessed anywhere in the Code through the window attribute (unless you have done something out of the box, such as declaring a local variable named window ). However, in other environments, this convenient property may be called something else (or even unavailable in programs ). If you need to access a global object without a hardcoded window identifier, you can perform the following operations in the function scope at any level:

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

This method can obtain global objects at any time, because it is called as a function (not constructed through new) in the function. this always points to the global object. In fact, this disease does not apply to the strict mode of ECMAScript 5. Therefore, in strict mode, you must take different forms. For example, if you are developing a JavaScript library, you can wrap your code in a real-time function, and then pass a reference pointing to this as a parameter of your real-time function from the global scope.

The above is the difference between global variables displayed in javascript and implicit global variables. The fundamental difference between the two is that explicitly declared variables are not configurable and cannot be deleted through the delete operator.

For more information about the differences between global variables and implicit global variables displayed in javascript, see PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.