简体   繁体   English

未使用var声明的JavaScript变量是否变为全局变量?

[英]Does a JavaScript variable not declared with var become global?

I have the following code in one file: 我在一个文件中有以下代码:

function refreshGridSuccess(responseText, entity) {

    oTable = $('#dataTable').dataTable({
        "sScrollX": "100%",

In another file I have: 在另一个文件中我有:

$('#detailData')
        .on('click', '.sort-up', function (event) {
            event.preventDefault();
            var column = $(this).closest('th'),
                columnIndex = column.parent().children().index(column.get(0));
            oTable.fnSort([[columnIndex, 'asc']]);
            return false;
        })

I have no definition for oTable except here. 除了这里,我没有oTable定义。 The scripts seem to work so does that mean that oTable was somehow made into a global variable? 脚本似乎工作,所以这意味着oTable以某种方式被制作成一个全局变量?

Now I am trying to start using Typescript and it will not accept Otable without it being declared. 现在我正在尝试开始使用Typescript,如果没有声明它就不会接受Otable Is there a way I can declare oTable as an object or do I have to declare it as an object to be the same type as is returned by $('#dataTable').dataTable({}) ? 有没有办法可以将oTable声明为对象,或者我必须将它声明为与$('#dataTable').dataTable({})返回的类型相同的对象oTable $('#dataTable').dataTable({})

If you're in the global scope then there's no difference. 如果你在全球范围内,则没有区别。 If you're in a function then "var" will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it). 如果你在函数中,那么“var”将创建一个局部变量,“no var”将查找范围链,直到它找到变量或命中全局范围(此时它将创建它)。

Found here: What is the purpose of the var keyword and when to use it (or omit it)? 在这里找到: var关键字的用途是什么以及何时使用它(或省略它)?

Yes, a variable not declared with var becomes global. 是的,未使用var声明的变量变为全局变量。 When used outside of a function's scope it is not necessarily required, however, using a globally defined variable which was defined in a function results in unpredictable results. 当在函数范围之外使用时,它不一定是必需的,但是,使用在函数中定义的全局定义的变量会导致不可预测的结果。

"Failure to declare the variable in these cases will very likely lead to unexpected results. For that reason, in ECMAScript 5 strict mode, assigning a value an undeclared variable inside a function throws an error." “在这些情况下未能声明变量很可能会导致意外结果。因此,在ECMAScript 5严格模式下,在函数内部分配未声明变量的值会引发错误。” - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var

You are correct. 你是对的。 Variable without a var declaration is treated as global. 没有var声明的变量被视为全局变量。 If you are in a browser, that scope is window 如果您在浏览器中,则该范围是window

Also note that Javascript variables are function-scoped. 另请注意,Javascript变量是函数范围的。 Meaning if you have a code like this: 意思是你有这样的代码:

function test()
{
   for (var key in someObject)
   {
      var foo = 'bar';
   }
}

It is equivalent to this: 它相当于:

function test()
{
   var foo, key;
   for (key in someObject)
   {
      foo = 'bar';
   }
}

It doesn't matter which block the var declaration is placed in, the actual declaration happens at the beginning of the function. var声明放在哪个块中无关紧要,实际声明发生在函数的开头。 This is also called variable hoisting if you want to google it. 如果你想google它,这也称为variable hoisting

Now about Typescript , I haven't had a chance to play with it. 现在关于Typescript ,我没有机会玩它。 But I assume you can still do var oTable; 但我认为你仍然可以做var oTable; right before you use the variable? 就在你使用变量之前?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在对象方法中声明为不带var的变量不会成为全局变量? - Why variable declared without a var in object method does not become a global variable? 为什么在for循环中使用javascript的var变量会变成全局变量? - Why var variable in javascript become global when used in for loop? 如何重置由关键字 var 声明的 javascript 变量? - how to reset javascript variable declared by keyword var? Javascript变量是否在被视为全局的函数内声明(无变量)? - Are Javascript variables not declared (no var) inside functions treated as global? JavaScript中的全局变量,不带var定义的变量 - Global Variable in JavaScript , variables defined without var Javascript / HTML变量未定义(全局变量) - Javascript/HTML variable is undefined (global var) 在Javascript中'var'声明的变量和'this'创建的属性之间有什么区别? - What's the difference between a 'var' declared variable and 'this' created property in Javascript? 如何使 Javascript 中的 function 变量变为全局变量? - How to make variable from function in Javascript become global? 在 if 语句中声明的 Javascript 变量不会更改全局变量 - Javascript variable declared in if statement wont change the global variable Angular 如何将外部 javascript 库绑定到已声明的 var? - How does Angular bind an external javascript library to a declared var?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM