指当一个人应该写window.X内置全局属性中的X(桌面)浏览器? [英] Should one write window.X when referring to a built-in global property X in the (desktop) browser?

查看:95
本文介绍了指当一个人应该写window.X内置全局属性中的X(桌面)浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,有几十个内置的全局属性中(桌面)浏览器。例如:

So, there are dozens of built-in global properties in the (desktop) browser. For instance:


  • 文件

  • 未定义

  • parseInt函数

  • JSON

  • 位置

  • 警报

  • setTimout


  • document
  • undefined
  • parseInt
  • JSON
  • location
  • alert
  • setTimout
  • etc.

当提到这些属性,应在明确地指出他们由$ P $全局属性pfixing他们的名字与窗口。?因此,例如:

When referring to those properties, should one explicitly note them as global properties by prefixing their name with window.? So, for instance:

var wrap = window.document.getElementById('wrap');

window.setTimeout(loop, 100);

var x = window.parseInt(input.value, 10);

我觉得有三个回答这个问题:

I think there are three answers to this question:


  1. 是的,你应该总是写 window.X 指的是全球性的时候。

没有,你不必写 window.X 。刚 X 的罚款。

No, you don't have to write window.X. Just X is fine.

这取决于财产。对于某些属性,使用 window.X ,对于一些其他属性使用 X 。 (如果这是你的回答,请详细说明。)

It depends on the property. For some properties, use window.X, for some other properties use X. (If this is your answer, please elaborate.)

那么,这是什么呢?

推荐答案

我会去3:没有窗口除了少数例外

I would go for 3: no window except for a few exceptions.

在浏览器中,窗口是指全球范围。 窗口。 window.prompt()是多余的。你可以用它来强调提示符()的法窗口对象。

In browsers, window refers to the global scope. window. as in window.prompt() is redundant. You could use it to emphasize that prompt() is a method of the window object.

我永远不会使用类似 window.Math window.NaN ,因为这些属性是全局对象有无关的窗口对象,它是顺便在浏览器中的全局对象。参见<一个href=\"http://www.herongyang.com/JavaScript/Built-In-Object-Global-Properties-and-Functions.html\">Global性能和功能定义在ECMAScript中。

I would never use something like window.Math or window.NaN because these properties are global objects that has nothing to do with the window object which is incidentally the global object in browsers. See also Global Properties and Functions Defined in ECMAScript.

如果您在指定的电流(本地)范围另一个变量提示,你需要在窗口。 preFIX以及得到提示对话框,如下所示:

If you have another variable in the current (local) scope named prompt, you would need the window. prefix as well to get the prompt dialog as in:

(function() {
   var prompt = "Give me your name!";
   var name = window.prompt(prompt, "your name");
})();

有关设置全局变量,您应该添加窗口。 preFIX以及满足喜欢的 JSLint的。 (否则,它会看起来像一个你已经忘记了 VAR 关键字,从而意外地在全球范围内泄漏的变量)​​:

For setting global variables, you should add the window. prefix as well to satisfy tools like jslint. (otherwise, it would look like a you have forgotten the var keyword and thereby accidentally leaks a variable in the global scope):

(function() {
   // "WRONG"
   somevar = 1;
   // You probably want to set a local variable, so should use:
   var somevar = 1;
   // take away the confusion, you really wanted to set a global variable:
   window.somevar = 1;
})();

一般情况下,省略窗口提高了可读性,考虑到下一个例子:

Generally, omitting window. improves readability, considering the next example:

window.setInterval(function() {
   var numA = window.parseInt(window.document.getElementById("numA").value, 10);
   var numB = window.parseInt(window.document.getElementById("numB").value, 10);
   window.document.getElementById("avg").value = window.Math.floor((numA + numB) / 2);
}, 1000);

这篇关于指当一个人应该写window.X内置全局属性中的X(桌面)浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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