/    Sign up×
Community /Pin to ProfileBookmark

Global Variable inside Function

I know, I know… Declare it ouside the function first… This is a total beginner question.

Well, I’m trying to write something more advanced and I’m trying to keep my page extremely dynamic. That means that I need variables to create themselves dynamically, because I can’t guarantee what will be on the screen (user generated).

For example:
I have dynamic layers that could be named anything and I need to store the height and width of them, since those are also dynamic.

I can use eval() to create dynamic variable names, but, used inside functions, it’s worthless if I can’t use them in other places. So now the information is stored dynamically but it can’t be retrieved because it was lost when the function ended.

“Return value” is out of the question unless you have a really creative way of doing it.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@cridleyOct 19.2007 — a class? with a name and x and y properties?

[CODE]
function cLayer(name,x,y) {
this.name=name;
this.x=x;
this.y=y;
}

Layer1 = new cLayer("whatever",12,31)

// access with
Layer1.x
//etc...

[/CODE]


You could have a global array of these or something. Depends on what you're trying to do really.
Copy linkTweet thisAlerts:
@KorOct 19.2007 — You may create global variables inside a function using the fact that global variables (and the functions as well) are [I]custom properties[/I] of the [B]window[/B] object:

<i>
</i>function createGlobals(name,val){
window[name]=val;
}
Copy linkTweet thisAlerts:
@Declan1991Oct 19.2007 — Can't you just leave var out of it too?
Copy linkTweet thisAlerts:
@Angry_Black_ManOct 20.2007 — You may create global variables inside a function using the fact that global variables (and the functions as well) are [I]custom properties[/I] of the [B]window[/B] object:

<i>
</i>function createGlobals(name,val){
window[name]=val;
}
[/QUOTE]


so this wouldnt have to be a function, right? you could simply declare your new global scope variable on the spot, right?

also, i never knew anything about this and its blowing my mind. how does this work? got any tech references so i can bone up??
Copy linkTweet thisAlerts:
@KorOct 20.2007 — To understand that you should think in JSON way.

Javascript is an Object Oriented language. Apart the DOM hierarchical structure, javascript is able to apply also the [B]object:property[/B] dual model. Everything can be an object in javascript, and any object can be a property of another object. Global variables and global functions are the properties of the root object [B]window [/B].
Copy linkTweet thisAlerts:
@KorOct 20.2007 — And here's an example of how a function can be treated as an object:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function callF(name){
window[name](name)
}
function foo(arg){
alert(arg)
}
function fee(arg){
alert(arg)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;span onclick="callF('foo')"&gt;call foo&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span onclick="callF('fee')"&gt;call fee&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MrAdunauthorOct 20.2007 — Wow, thanks a lot guys.

I actually ended up using both cridley's and Kor's methods. Things start getting really sticky when you're working in OOP and nothing is static. You start getting some really weird behavior.

And eval()'s nested in if statements, calling functions from objects within eval()'s that you don't know the names of, determining if a variable (which you don't know the name of) gets really... headache inducing. But hey, it's really expanding my logic skill. I'd have to say that Rubik's cubes are simpler for me at this point.

The scary part is I need to get this done very very quickly and I CANNOT take a break from it. I went one night without looking at it, and I had to spend about 45 minutes when I sat back down to retrace my steps to where I was and where I was going.

So thanks, my brain was frying over here from staring at it too long.
Copy linkTweet thisAlerts:
@KorOct 20.2007 — No, you should not use eval(). It's evil. You should use JSON and/or DOM
Copy linkTweet thisAlerts:
@MrAdunauthorOct 20.2007 — Ehhh, it's making for easy visual representation of what I'm doing. I can look at my code, if I see an eval() I know it's representing a variable who's name I don't know.

And I'm not quite sure how to get around the "I don't know the variable name" part with DOM or JSON. Honestly, I've never read up on JSON.

So how would you replace code like this?

[CODE]if (eval("undefined==window." + "foo" + id)){
var temp = document.getElementById(id + "Title").value
} else {
var temp = eval("foo" + id + ".title")
}
[/CODE]
Copy linkTweet thisAlerts:
@KorOct 21.2007 — Could be something like:
<i>
</i>var temp;
if(typeof window['foo'+id]=='undefined'){
temp=document.getElementById(id + "Title").value;
}
else{temp=document.getElementById('foo'+id).title}
×

Success!

Help @MrAdun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...