关键字定义多个变量

关键字定义多个变量

本文介绍了在javascript中使用一个`var`关键字定义多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源代码中有一行是别人写的:

I have a line in my source code written by someone else:

var campaignLimits = 10, campaignsArray = new Array();

我只是想知道,这里的campaignsArray是成为全局变量,还是var也适用于campaignsArray?

I just wanted to know, whether campaignsArray here becomes global variable, or the var applies to campaignsArray as well?

推荐答案

假设你没有使用过任何编程模式,如果它写在一个函数中,那么它不是全局的.

Assuming you have not used any programming pattern, If its written inside a function then its not global.

(function() { var campaignLimits = 10, campaignsArray = new Array(); })();

正如@phoa 所评论的,它与

as @phoa commented it is same as

(function() { var campaignLimits = 10; var campaignsArray = new Array(); })();

在您的控制台中尝试一下,看看您是否能够访问campaignArray.

Try it in your console and see whether you will be able to access campaignsArray.

这篇关于在javascript中使用一个`var`关键字定义多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 00:44