473,386 Members | 1,924 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,386 software developers and data experts.

Locally var.s affect global ones

Say I have script that looks a bit like this:
Array.prototype.getValue = function(ind, dir) {
if (dir !=0) ind[ind.length-1] += dir;
// more script
}

screens = [...// a big array
fnd = [1,2,3];
// 1.
a = findStart(fnd);
// 2.
b = findEnd(fnd);

findStart = function(cs) {
return screens.getValue(cs, 1);
}

findEnd = function(cs) {
return screens.getValue(cs, -1)
}
As I go from 1. to 2. the value of fnd changes. I don't understand how a
locally scoped variable might be affecting a global one.

Andrew Poulos
Sep 7 '05 #1
1 1166


Andrew Poulos wrote:
fnd = [1,2,3];
// 1.
a = findStart(fnd);
// 2.
b = findEnd(fnd);

As I go from 1. to 2. the value of fnd changes. I don't understand how a
locally scoped variable might be affecting a global one.


What you have above is a variable fnd which contains a reference to an
array object and whereever you pass fnd to you pass in the reference to
that array object and any function then operates on the array object fnd
references.
So it is not suprising in JavaScript that an object or array object is
manipulated if you pass it to a function or method, no copies of objects
respectively array objects are created, instead references to objects
are passed around. Pretty much the same as in Java where you have value
types and reference types.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 7 '05 #2

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

Similar topics

3
by: Aardwolf | last post by:
I've had PHP on my Win2K server running IIs 5 for about 2 weeks now. I have an annoying problem in that I can not run the php scripts/pages locally, instead they open up in the browser as a...
88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
18
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them...
2
by: Abdessamad Belangour | last post by:
Hi all, and thanks for previous answers (especially Nicholas Paldino) An assembly is composed of a set of modules. The module class has a method for reading global methods GetMethods(). My...
2
by: Raterus | last post by:
Hi, I'm after this setup, and I'm just wondering if this is even feasible. I'd like to store my asp.net project remotely, on a network drive (all the project files, everything), But I want to be...
10
by: Ronnie | last post by:
I've created a simple web application using VS2005 Beta 2. Basically, I've added a Web Form and a Global.asax file. In my Global.asax, I create a method like: public static void TestGlobal()...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: | last post by:
Hello, This is a very belated followup to the below issue, I am the original poster. I recently was creating a new OU structure and new security policy and during testing it was noticed that in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.