简体   繁体   English

在JavaScript中创建全局变量时是否需要指定对象类型?

[英]Do I need to specify object type when creating globals in Javascript?

I have the following code: 我有以下代码:

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

I would like to declare oTable as a global but I am a bit confused. 我想将oTable声明为全局表,但我有些困惑。 With javascript how do I do this and do I have to specify the object type when I declare it as a global? 使用javascript如何做到这一点,并且在将其声明为全局对象时是否必须指定对象类型?

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'
}

You can declare it globally like this without any problem. 您可以像这样全局地声明它。 Var is generic type and any type could be assigned to it. Var是通用类型,可以将任何类型分配给它。

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

function someFunction()
{

}

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. window是指浏览器javascript中的全局对象。

Unless that declaration is inside of a function, oTable is already a global variable. 除非该声明在函数内部, oTable已经是全局变量。 Also, Javascript is dynamically-typed, which means you're not can't define the variable type. 另外,JavaScript是动态类型的,这意味着您无法定义变量类型。

For more information on global variables in Javascript, check out this article. JavaScript中的全局变量的更多信息,请查看文章。
For more information on static vs dynamic typed, check out this question. 有关静态类型和动态类型的更多信息,请查看问题。

You can't declare a type. 不能声明类型。

Unless the variable is defined within a function using var it'll be a global. 除非使用var在函数内定义变量,否则它将是全局变量。

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

相关问题 我什么时候需要指定JavaScript协议? - When do I need to specify the JavaScript protocol? 我是否需要在 javascript/typescript 中为匿名函数指定返回类型? - Do I need to specify a return type for an anonymous function in javascript / typescript? 如何在创建JavaScript日期时指定时区? - How do I specify the time zone when creating a JavaScript Date? 我需要在Javascript闭包中指定参数吗 - Do I need to specify parameter in Javascript closure 在Java中使用ForEach填充数组对象时是否需要定义数组对象? - Do I need to define an array object when populating it with a ForEach in Javascript? 在动态创建脚本时,我们还需要“script.type ='text / javascript”吗? - Do we still need “script.type='text/javascript” when creating a script dynamically? 刚刚看到了用JavaScript创建的工厂样式对象(没有文字)。 我还需要知道什么? - Just seen a factory style object creating in JavaScript (without literals). What else do I need to know? 指定在另一个文件中声明的全局变量的类型 - Specify type of globals declared in another file 当 object 不可调用时,如何使用 python selenium “单击”? 我在这里需要 javascript 吗?如何? - How do I “click” with python selenium when object is not callable? Do I need javascript here, and how? 我需要在javascript中声明一个对象吗? - Do I need to declare an object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM