Declaring public variables inside a function

S

Sister Ray

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.
 
T

Tom de Neef

Sister Ray said:
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
 
G

Gregor Kofler

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
 
D

Dr J R Stockton

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, .....|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top