[英]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.