473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring public variables inside a function

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
4 4345
"Sister Ray" <ca********@gma il.comschreef in bericht
news:c7******** *************** ***********@59g 2000hsb.googleg roups.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
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.""""""""@ph ysics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demo n.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi,
JavaScript, .....|

Sep 22 '08 #3
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.b ar = 42;
}

function foo2() {
window.alert(my globalscope["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
thank's it does work.
Sep 23 '08 #5

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

Similar topics

2
2129
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example...
2
53860
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date; DECLARE end_date date; SET start_date = '2005-01-01'
2
1453
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2 of these may or may not be used depending on a couple of IF...Then's further down the page... What I'd like to know is - should I declare ALL of my...
3
1803
by: Joe Fallon | last post by:
I have a Shared varibale in a base class and all the Shared methods in the sub-classes use it (and CHANGE it). I thought I was saving myself the "trouble" of Dimming this variable inside each Shared method. But now I wonder if I will have a problem in a multi-user environment with code that changes this variable. Can someone please review...
6
3500
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en error saying to change Public to Dim. When I do, it will say that Test1 is an unused local variable.
8
7500
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
8
2922
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to call the function, EncodeAsnUser. And it's returning OK but when I display the decoded data in another part of my application it shows no data has...
17
2119
by: iesvs | last post by:
Hello, i want to create c++ libraries (for my use) but I don't know how I must do to have headers which contain only the public declarations. I used in C to put the private declarations in *.c and the public declaration in *.h. But how to do that in c++. If someone could help me, he would be nice. Thank you.
2
1664
by: MLH | last post by:
I'm wondering about Public VS Global kewords that can be used in procedures inside global modules saved/viewd in the modules tab of the database window. Is it pretty much the same thing to use one or the other when declaring a global variable? If not, what is (are) the major difference(s) ?
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.