By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Declaring public variables inside a function

P: n/a
I've searched for a way to this in a lot of forums. Haven't found a
solution for this, however I've found a work around.
Declaring a public object and then adding the variables i want as
members of thta object, like this

<script>
function myobject(){}

var a = new myobject();

function foo()
{
a.var1 = "some string";
}

function foo2()
{
//a.var1 exists in foo2
alert(a.var1);
}
//and outside the function
alert(a.var1);

this spends a litle more memory but does what i wanted. apart from
obvious "why would i want to this", does anybody have a better
solution for this?

Best Regards.
Sep 22 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Sister Ray" <ca********@gmail.comschreef in bericht
news:c7**********************************@59g2000h sb.googlegroups.com...
I've searched for a way to this in a lot of forums. Haven't found a
solution for this, however I've found a work around.
Declaring a public object and then adding the variables i want as
members of thta object, like this

<script>
function myobject(){}

var a = new myobject();

function foo()
{
a.var1 = "some string";
}

function foo2()
{
//a.var1 exists in foo2
alert(a.var1);
}
//and outside the function
alert(a.var1);
I thought that any variable NOT declared will have global scope.
function foo() {var1 = "some string"}
will result in var1 as a variable with global scope whose value will be
assigned when foo() is called.
You may want to check this.
Tom

Sep 22 '08 #2

P: n/a
On Sep 22, 7:49*pm, "Tom de Neef" <tden...@qolor.nlwrote:
I thought that any variable NOT declared will have global scope.
*function foo() {var1 = "some string"}
will result in var1 as a variable with global scope whose value will be
assigned when foo() is called.
ISTM that var1 is created the first time foo is called, and I suppose
assigned every time foo is called. Before foo is first called, var1
does not exist.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi,
JavaScript, .....|

Sep 22 '08 #3

P: n/a
Sister Ray meinte:
I've searched for a way to this in a lot of forums. Haven't found a
solution for this, however I've found a work around.
Searched for what? It's pretty annoying to make up problem descriptions
by combining subjects and message texts.

Just *don't* declare anything - and the variable will end up in the
global scope. That's perhaps /the/ problem of JS.
Declaring a public object and then adding the variables i want as
members of thta object, like this

<script>
function myobject(){}

var a = new myobject();
Constructors should start with capital letters.

[useless stuff snipped]

If you insist:

<script type="text/javascript">
var myglobalscope = this;

function foo() {
myglobalscope.bar = 42;
}

function foo2() {
window.alert(myglobalscope["bar"]);
}

foo();
foo2();
</script>

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 22 '08 #4

P: n/a
thank's it does work.
Sep 23 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.