search
HomeWeb Front-endJS Tutorialjavascript displays the difference between global variables and implicit global variables

This article mainly introduces the difference between javascript display global variables and implicit global variables. Friends in need can refer to it

In JavaScript, there are two ways to declare global variables

  • Use var to display declared global variables

  • Do not use var to declare implicit global variables

The difference between the two It depends on whether it can be deleted through the delete operator

Let’s first look at a piece of code

var a = 'a'; // 显式声明的全局变量
b = 'b'; // 隐式声明的全局变量
 
console.log(a); // a
console.log(b); // b
console.log(window.a); // a
console.log(window.b); // b

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

Try to delete using delete

// 显式声明的全局变量不能被删除
delete a; // 返回 false 
 
// 隐式声明的全局变量可以被删除
delete b; // 返回 true 
 
// 删除情况
console.log(typeof a); // string
console.log(typeof b); // undefined

## The #delete operator can delete the attributes of an object, but if the attribute is a non-configurable attribute, it will return false when deleted (an exception will be thrown in strict mode)

This means using Variables declared with var are not configurable. Use getOwnPropertyDescriptor to obtain an object describing the property characteristics 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 is the explicit declaration The variables are not configurable and cannot be deleted through the delete operator

It should be noted that once the configurable value is false, the object describing the attribute characteristics cannot be modified, so the declared global variables cannot be displayed by modifying the attribute descriptor. Can be deleted by delete, but conversely, implicitly declared global variables cannot be deleted by delete

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

The following are the additions of other netizens

JavaScript Global Variables and Implicit Global Variables

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.

1. Global variables created through var (created in any program other than functions) cannot be deleted.

2. 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 the browser, the global object can be used in any part of the code through the window attribute. Positional access (unless you do something outrageous, like 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 This method can obtain the global object at any time, because it is called as a function in the function (not constructed through new), and 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 difference between javascript display 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 the delete operator

More javascript display For related articles on the difference between global variables and implicit global variables, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C++ 函数的局部变量和全局变量有什么区别?C++ 函数的局部变量和全局变量有什么区别?Apr 19, 2024 pm 03:42 PM

C++局部变量和全局变量的区别:可见性:局部变量仅限于定义函数,而全局变量在整个程序中可见。内存分配:局部变量在栈上分配,而全局变量在全局数据区分配。作用域:局部变量在函数内,而全局变量在整个程序中。初始化:局部变量在函数调用时初始化,而全局变量在程序启动时初始化。重新创建:局部变量在每次函数调用时重新创建,而全局变量仅在程序启动时创建。

go语言有静态全局变量么go语言有静态全局变量么Jul 11, 2023 pm 03:37 PM

go语言没有静态全局变量,它使用了一种更为灵活的方式来处理全局变量的需求,全局变量通常是在包级别被声明,也就是在函数外部声明的变量,这些全局变量在整个包中均是可见的,可以在包中的任何函数中使用。

php request什么意思php request什么意思Jul 07, 2021 pm 01:49 PM

request的中文意思为“请求”,是php中的一个全局变量,是一个包含了“$_POST”、“$_GET”和“$_COOKIE”的数组。“$_REQUEST”变量可以获取POST或GET方式提交的数据、COOKIE信息。

在JavaScript中实现全局变量的安全性在JavaScript中实现全局变量的安全性Jun 15, 2023 pm 10:33 PM

随着JavaScript的流行,越来越多的网站和应用程序都依赖于JavaScript。然而,JavaScript中全局变量的使用可能存在安全问题。在此文中,我将介绍如何在JavaScript中实现全局变量的安全性。避免使用全局变量最好的方法是避免使用全局变量。在JavaScript中,所有变量都默认为全局变量,除非它们在函数中声明。因此,应尽可能使用局部变量

Golang函数的全局变量和局部变量的数据竞争分析Golang函数的全局变量和局部变量的数据竞争分析May 21, 2023 am 08:19 AM

Golang是一种强类型编程语言,具有高效、简洁、并发等特点,因此逐渐受到了越来越多的开发者的青睐。而在Golang的开发中,函数的全局变量和局部变量往往会涉及到数据竞争的问题。本文将从实际编码的角度,对Golang函数中全局变量和局部变量的数据竞争问题进行分析。一、全局变量的数据竞争Golang全局变量在所有函数中均可以访问,因此如果不进行严谨的设计和编码

C程序中全局变量的重新声明C程序中全局变量的重新声明Sep 20, 2023 pm 10:29 PM

我们将了解在不初始化的情况下重新声明全局变量、通过初始化重新声明全局变量、重新声明全局变量并初始化两次时,C和C++的行为有何不同。另外,我们将使用局部变量重复上述组合。1.A)C程序:重新声明全局变量而不进行初始化#include<stdio.h>intvar;intvar;intmain(){&nbsp;&nbsp;printf("Var=%d",var);&nbsp;&nbsp;return0;}输出Var=0B)C++程序:

golang函数能否在goroutine中直接访问全局变量?golang函数能否在goroutine中直接访问全局变量?May 01, 2024 pm 05:51 PM

是的,Go函数在Goroutine中默认情况下可以直接访问全局变量。原因:Goroutine继承创建它的Goroutine的内存空间,包括对全局变量的访问权限。

php全局变量都有哪些php全局变量都有哪些Aug 01, 2023 pm 01:21 PM

php全局变量有:1、$_SERVER,当前脚本运行的服务器和执行环境信息的超全局变量;2、$_GET,通过GET方法传递给当前脚本的变量的关联数组;3、$_POST,通过POST方法传递给当前脚本的变量的关联数组;4、$_SESSION,存储当前会话中用户相关信息;5、$_COOKIE,通过HTTP Cookie传递给当前脚本的变量的关联数组;6、$_FILES等等。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function