如何以正确的方式在javascript中声明全局变量?(How to declare global variables in javascript the right way?)

系统教程 行业动态 更新时间:2024-06-14 16:55:57
如何以正确的方式在javascript中声明全局变量?(How to declare global variables in javascript the right way?)

检查一个网站我已经遇到了这个代码;

if (!window.homePosition) window.homePosition = $('#sticky-container').offset().top;

在这两种方式中声明全局变量有什么不同吗?

window.homePosition = 400;

要么

var homePosition = 400;

为什么你认为以前的开发人员使用这种表示法?

Checking a website I have come accross with this code;

if (!window.homePosition) window.homePosition = $('#sticky-container').offset().top;

is there any difference in declaring the global variable in this two ways?

window.homePosition = 400;

or

var homePosition = 400;

Why do you think the previous developer used this notation?

最满意答案

是的,有两个不同之处,但实际上它们通常不是大的。

你有3个陈述

var a=0;

...在全局执行上下文的变量对象上创建一个变量,它是全局对象,在浏览器上别名为window (并且是一个DOM窗口对象,而不仅仅是非浏览器上的泛型对象)实现)。 符号window本身实际上是它用于指向自身的全局(窗口)对象的属性。

所有这一切的结果是:它在window上创建一个您无法删除的属性。 它也是在第一行代码运行之前定义的(参见下面的“当var发生时”)。

请注意,在IE8及更早版本中,在window上创建的属性不可枚举 (不会显示在for..in语句中)。 在IE9,Chrome,Firefox和Opera中,它是可枚举的。


a=0;

... 隐式地在window对象上创建一个属性。 由于它是普通属性,您可以删除它。 我建议要这样做,以后读取你的代码的任何人都不清楚。

有趣的是,再次在IE8和更早版本中,属性创建不可枚举 (不会出现在for..in语句中)。 这很奇怪,特别是考虑到以下情况。


window.a=0;

...显式地在window对象上创建一个属性。 由于它是普通属性,您可以删除它。

在IE8及更早版本以及我尝试过的其他所有浏览器中,此属性都是可枚举的。

Yes, there are two differences, though in practical terms they're not usually big ones.

Your have 3 statements

var a=0;

...creates a variable on the variable object for the global execution context, which is the global object, which on browsers is aliased as window (and is a DOM window object rather than just a generic object as it would be on non-browser implementations). The symbol window is, itself, actually a property of the global (window) object that it uses to point to itself.

The upshot of all that is: It creates a property on window that you cannot delete. It's also defined before the first line of code runs (see "When var happens" below).

Note that on IE8 and earlier, the property created on window is not enumerable (doesn't show up in for..in statements). In IE9, Chrome, Firefox, and Opera, it's enumerable.


a=0;

...creates a property on the window object implicitly. As it's a normal property, you can delete it. I'd recommend not doing this, it can be unclear to anyone reading your code later.

And interestingly, again on IE8 and earlier, the property created not enumerable (doesn't show up in for..in statements). That's odd, particularly given the below.


window.a=0;

...creates a property on the window object explicitly. As it's a normal property, you can delete it.

This property is enumerable, on IE8 and earlier, and on every other browser I've tried.

更多推荐

本文发布于:2023-04-10 11:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0c9510ad7366542a51fa3bb1a580a8ad.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局变量   声明   正确   方式   variables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!