0

I have the following code:

var oTable = $('#dataTable').dataTable({
 ...

I would like to declare oTable as a global but I am a bit confused. With javascript how do I do this and do I have to specify the object type when I declare it as a global?

jeff
  • 8,300
  • 2
  • 31
  • 43

5 Answers5

1

Put simply, you don't. You just declare the variable as you did, and use it wherever you need.

When you'll want to use a global variable in a function, you can simply type its name, without declaring it first.

Example

var MyVar = $('#dataTable').dataTable(); // This is a global variable. Notice that you don't specify a type, as JavaScript is not strongly typed

function MyFunction() {
  var InternalVar = MyVar; // Here you take the value from the global variable, i.e. the datatable
}

function MyOtherFunction() {
  var MyVar = 'This is a string';

  var InternalVar = MyVar; // Here you take the value from the LOCAL variable, which you declared just above, i.e. 'This is a string'
}
Diego
  • 7,312
  • 5
  • 31
  • 38
  • You have declare MyVar as 'Something'. But in my case it's an object (datatable). Do I have to declare it as a string like you did ? –  Aug 08 '12 at 15:15
  • No, I used a string just as an example. JavaScript is not strongly typed, therefore you can declare a variable as anything you like. I'll update the example using your code to make it clearer. – Diego Aug 08 '12 at 15:18
1

You can declare it globally like this without any problem. Var is generic type and any type could be assigned to it.

var oTable = $('#dataTable').dataTable({......

function someFunction()
{

}
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Global variables are not actually variables, but properties of the global object. They can be explicitly attached as follows:

window.oTable = $('#dataTable').dataTable({});

window refers to the global object in browser javascript.

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • It's not a string however :-( –  Aug 08 '12 at 15:16
  • It's anything you want. On assignment time, it'll become an object. – Alfabravo Aug 08 '12 at 15:17
  • Do I need to specify window? Can I just use window.otable = null; –  Aug 08 '12 at 15:18
  • 1
    @Anne oh right, the value doesn't matter. I was just too lazy to copy paste your value.. will edit – Esailija Aug 08 '12 at 15:20
  • Window.oTable is an example. The thing is, there're contexts above functions that work like a global scope. So, if you declare 'var yaddaYadda = $("#yadda").jquerySomething...' OUTSIDE any function or code block, it will be seen by ALL methods in ALL scripts loaded for that page. – Alfabravo Aug 08 '12 at 15:22
0

Unless that declaration is inside of a function, oTable is already a global variable. Also, Javascript is dynamically-typed, which means you're not can't define the variable type.

For more information on global variables in Javascript, check out this article.
For more information on static vs dynamic typed, check out this question.

Community
  • 1
  • 1
jeff
  • 8,300
  • 2
  • 31
  • 43
0

You can't declare a type.

Unless the variable is defined within a function using var it'll be a global.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302