条款“全球财产”和“全局变量”同义词? [英] Are the terms "global property" and "global variable" synonyms?

查看:99
本文介绍了条款“全球财产”和“全局变量”同义词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全局对象用作顶级词汇环境(如果您愿意,则作为范围链的顶部)。这意味着可以通过直接引用(如变量)访问全局属性:

  //全局代码
this。 foo = 1; //创建全局属性
foo //通过直接引用访问全局属性

这也意味着全局变量可以通过属性引用来访问:

  //全局代码
var foo = 1; //创建全局变量
this.foo //通过属性引用访问全局变量



< hr>

解释1



现在,基于上述信息,似乎使用术语全局变量是一个全局属性,这意味着两个术语代表完全相同的一组全局绑定。然而,使用 var 创建的全局变量之间存在两个区别:




c>,例如 var foo = 1; 以及通过赋值创建的全局属性,例如 this.foo = 1;


  1. 全局变量静态作用域,而全局属性动态添加到全局环境中:

      foo // => undefined 
    bar //引发ReferenceError

    var foo = 1;
    this.bar = 1;

    因此,全局变量在程序评估之前绑定,而全局属性为在计划评估过程中,在评估分配时绑定。全局变量是不可配置的,即它们不能被删除(更具体地说,它们相应的绑定不能随后从环境中移除),而创建的全局属性

      //名称foo和bar被绑定到 全球环境
    var foo = 1;
    this.bar = 1;

    //绑定bar可以随后从全局环境中删除
    delete this.bar;

    //绑定foo不能随后删除


这就是说,应该指出的是可以创建不可配置的全局属性:

  Object.defineProperty(this,'bar',{value:1}); // b 
$ b $ h
$ b

解释2



现在,基于这些新信息,可以说只有静态范围的全局绑定可以被称为全局属性和全局变量,而动态添加的全局绑定仅仅是全局属性,但不包括全局变量,这意味着术语全局变量表示由术语全局属性表示的集合的子集,如下所示:


所有全局变量都是全局属性

只有静态范围的全局属性是全局变量






那么,哪一种解释是正确的?这两个术语是否代表相同的一组绑定,或者是另一个绑定的一个子集?




问题



我确实理解术语全局属性 - 全球属性是全局对象的属性。但是,术语全局变量似乎不明确。有些人使用它作为全局属性的同义词,而另一些人则将其定义为通过 var 语句定义的全局属性。我的问题的目的是要确定这两个含义中的哪一个是正确的 解决方案

区分附加到 window 的变量和属性的边界情况。



如果你想真的很迂腐,我想你可以认为全局范围既是一个功能范围 a其中窗口对象的范围使用与程序中提供的隐藏配置设置相同的隐藏配置属性(例如:可以重新分配但不能删除的变量)进行扩展。所以从这个意义上说,就功能而言,它们是不同的,并且反映了属性和变量的属性,全局范围。



引用它们是完全的好的。

但是大多数人甚至都没有认识到差异,更不用说区分这两个术语。

即使是那些重要的JS作者也提到了设置一个全局变量意外地忽略了 var ,实际上,JS会缩放函数作用域,如果它使全局范围没有触及那个名字,它会在这个数据后附加一个全局属性,而不是一个全局变量



但是,这真的让我们陷入了困境 - 一个强大而稳定可靠的JS应用程序,与其他应用程序一起生活在一个现代网页上,实际上不应该是太关注差异。

目标是使用尽可能少的全局属性和变量在这种情况下,可能会发生变化。



此外,变量/财产冲突的危险是相同的,无论它是哪一个。

变量对 delete 是免疫的,但是任何有用的程序都会去 delete 一个属性,它甚至从未设置过所以我个人认为理解边缘情况是很好的,但我也认为尽管我的学生想要认同它们有区别,但他们并不是同时存在的,我的实用主义者不寒而栗,想到一个人们正在积极利用全球范围的世界,这对他们产生了很大的影响。

The global object serves as the top-level lexical environment (the top of the scope-chain, if you will). This means that global properties may be accessed via direct references (like variables):

// global code
this.foo = 1;        // creating a global property
foo                  // accessing the global property via a direct reference

This also means that global variables may be accessed via property references:

// global code
var foo = 1;         // creating a global variable
this.foo             // accessing the global variable via a property reference


INTERPRETATION 1

Now, based on the above information, it would seem that it would be appropriate to use the terms "global variable" an "global property" interchangeably, meaning that both terms represent the exact same set of global bindings.


However, there are two differences between a global variable created using var ,e.g. var foo = 1;, and a global property created through an assignment, e.g. this.foo = 1;:

  1. Global variables are statically scoped, whereas global properties are dynamically added to the global environment:

    foo // => undefined
    bar // throws ReferenceError
    
    var foo = 1;
    this.bar = 1;
    

    So, global variables are bound before program evaluation, whereas global properties are bound during program evaluation, when the assignment is evaluated.

  2. Global variables are non-configurable, i.e. they cannot be deleted (more specifically, their corresponding bindings cannot be removed from the environment subsequently), whereas global properties created through assignment are configurable.

    // the names "foo" and "bar" are bound to the global environment
    var foo = 1;
    this.bar = 1;
    
    // the binding "bar" can be removed from the global environment subsequently 
    delete this.bar; 
    
    // the binding "foo" cannot be removed subsequently
    

That being said, it should be noted that it is possible to create non-configurable global properties:

Object.defineProperty( this, 'bar', { value: 1 }); // non-configurable by default


INTERPRETATION 2

Now, based on this new information, one could say that only statically scoped global bindings may be referred to as both global properties and global variables, whereas dynamically added global bindings are merely global properties, but not global variables, meaning that the term "global variable" represents a subset of the set represented by the term "global property", as in:

All global variables are global properties
Only statically scoped global properties are global variables


So, which interpretation is correct? Do both terms represent the same set of bindings, or is one a subset of the other?


THE QUESTION

I do understand the term "global property" - a global property is a property of the global object. However, the term "global variable" appears to be ambiguous. Some use it as a synonym for "global property", while others define it to mean a global property which has been defined via a var statement. The intent of my question is to determine which of these two meanings is correct

解决方案

Well, you already know everything I would have said to differentiate between the edge-cases of variables and properties which are attached to window.

If you wanted to get really, really pedantic, I suppose that you could consider the global scope to be both a functional-scope and a scope in which the window object is extended with properties using the same hidden configuration settings as what is provided within the program (eg: vars can be reassigned but not deleted). So in that sense, as far as functionality is concerned, they are different, and reflect attributes of properties and variables, globally scoped.

And referring to them as such is totally fine.
But the majority of people out there don't even recognize the difference, let alone differentiate between the two terms.
Even the important JS authors out there have referred to setting a global variable accidentally, by omitting var, when really, JS scales the function scopes, and if it makes it to the global scope without hitting that name, it appends a global property with that data, rather than a global variable.

But that really sort of brings us to the crux -- a strong and stable and reliable JS application, living on a modern webpage along with other applications, really shouldn't be too concerned with the differences.
The goal is to use as few global properties and variables as possible, in that case.

Moreover, the danger of variable/property collision is the same, regardless of which it is.
Variables are immune to delete, but what are the chances that any useful program is going to delete a property it's never even set?

So personally, I think it's good to understand the edge-cases, but I also think that while the pedant in me wants to agree that there's a difference, and they are not coterminous, the pragmatist in me shudders to think of a world where people are actively using the global scope to the extent where this makes a large difference to them.

这篇关于条款“全球财产”和“全局变量”同义词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆