473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

create global variable from inside the function?

Hi

Is this possible?

One thing I can do is create the global array and add the new item to it
from inside the function but that's not what I'm looking for.

Thank you

Ralph
Oct 24 '06 #1
3 11607

Ralph wrote:
Hi

Is this possible?

One thing I can do is create the global array and add the new item to it
from inside the function but that's not what I'm looking for.
function (){
window.anArray = ['foo', 'bar', 1, 2, 3];
}

Maybe you'd prefer:

var _global = this;
function (){
_global.anArray = ['foo', 'bar', 1, 2, 3];
}
It is a good idea to avoid global variables where possible, make them
properties of some other object - a kind of namespace to keep your
stuff from colliding with other stuff.
--
Rob

Oct 24 '06 #2
ASM
Ralph a écrit :
Hi

Is this possible?
var c = 'bonjour';

function glob() {
var a = 'hello';
b = 'saluti';
}

glob();

alert(a); // undefined
alert(b); // saluti
alert(c); // bonjour

all variables in JS are global
except those declared in a function and prefixed with var
One thing I can do is create the global array
do you mean array of globals ?
and add the new item to it
from inside the function but that's not what I'm looking for.
function arr() {
tablo = new Array('one','two');
}
function arrAdd() {
if( typeof (tablo) == 'undefined') arr();
for(var i=0; i<arguments.length; i++)
tablo[tablo.length] = arguments[i];
}

arrAdd('three','for','five');

alert(tablo[4]); // five

--
ASM
Oct 24 '06 #3
ASM wrote:
Ralph a écrit :
>Hi

Is this possible?

var c = 'bonjour';

function glob() {
var a = 'hello';
b = 'saluti';
}

glob();

alert(a); // undefined
alert(b); // saluti
alert(c); // bonjour

all variables in JS are global
except those declared in a function and prefixed with var
>One thing I can do is create the global array

do you mean array of globals ?
>and add the new item to it from inside the function but that's not
what I'm looking for.

function arr() {
tablo = new Array('one','two');
}
function arrAdd() {
if( typeof (tablo) == 'undefined') arr();
for(var i=0; i<arguments.length; i++)
tablo[tablo.length] = arguments[i];
}

arrAdd('three','for','five');

alert(tablo[4]); // five
Thank you all. That what I was looking for.
Ralph
Oct 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Japhy | last post by:
Hello, I'm a newbie using PHP. Having some trouble using a variable within a function. I use some mysql code to assign a value to $myvariable. I verify that it is getting the value with an...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
0
by: Bryan Parkoff | last post by:
I know that after optimization is done under C++ compiler's option, it always align byte and word into dword inside struct. What about global variable in this slope? If global variable has done...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
9
by: ruca | last post by:
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET application? Example: var aux=null; function...
2
by: mars | last post by:
In my application, global variable A, during its construction, depends on another global variable, say B. However, variable B may have not been initialized by then. How can I make sure that...
8
by: Tuxedo | last post by:
How can I modify any one of these global variable identified via a function argument? var x1 = "bla"; var x2 = "bla"; var x3 = "bla"; function modify(variable) { ???? ???? = "blabla":
3
by: zensunni | last post by:
Ok, this is my code for my function: // Puke - new pointers only point to mTemp Object &Coordinate::stripObject(std::string name) { for ( int i=0; i < mItems.size(); i++ ) { if (...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.