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. The scripts seem to work so does that mean that oTable
was somehow made into a global variable?
Now I am trying to start using Typescript and it will not accept Otable
without it being declared. 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({})
?
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).
Found here: What is the purpose of the var keyword and when to use it (or omit it)?
Yes, a variable not declared with var becomes global. 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." - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/var
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With