defining multiple variables with one `var` keyword in javascript [duplicate]

那年仲夏 提交于 2020-12-16 07:48:30

问题


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

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

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


回答1:


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(); })();

as @phoa commented it is same as

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

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



来源:https://stackoverflow.com/questions/40883472/defining-multiple-variables-with-one-var-keyword-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!