Dormilich 8,658
Recognized Expert Moderator Expert
Hi,
I’m currently doing a fun project, where I stumble upon the quite annoying fact, that between some methods the arrays (they are the parameters) are passed by reference, which screws up the logic.
does anyone have an idea, how I can pass the arrays by value? - GAME.MagicSquare.prototype.createTable = function ()
-
{
-
var doc = document,
-
// create various HTMLElements
-
rows = this.rows,
-
cols = this.columns,
-
arr = [],
-
i, j, tr_i, td_j, num, range, values;
-
-
range = (rows * cols);
-
// create the result array necessary for win test
-
this.result = [];
-
for (i = 1; i < range; i += 1) {
-
this.result.push(i);
-
}
-
// create mixed and valid configuration
-
do {
-
// couldn't get it to be passed by value
-
for (i = 0; i < range; i += 1) { // fix for problem #1
-
arr.push(i);
-
}
-
values = this.scramble(arr); // <-- problem #1
-
}
-
while (this.unsolvable(values)); // <-- problem #2
-
-
// create the table
-
-
return table;
-
};
-
-
GAME.MagicSquare.prototype.scramble = function (a)
-
{
-
var i, index,
-
values = [];
-
-
for (i = a.length; i--;) {
-
index = Math.round(Math.random() * 100 * i) % i;
-
values.push(a.splice(index, 1)[0]);
-
}
-
// a is now [] and in the calling function arr also is []
-
return values;
-
};
-
-
GAME.MagicSquare.prototype.unsolvable = function (a)
-
{
-
var i, j,
-
inversion = 0,
-
l = a.length,
-
zero = a.indexOf(0),
-
cols = this.columns;
-
// move the 0 to the last row
-
j = l - cols;
-
while (zero < j) {
-
a[zero] = a[zero + cols];
-
a[zero + cols] = 0;
-
zero += cols;
-
}
-
// again, in the calling function, the array (named values) always has the 0 in the last "row"
-
// determine number of values, that are placed after its successors
-
for (i = 0; i < l; i += 1) {
-
for (j = i + 1; j < l; j += 1) {
-
if (a[i] > a[j] && a[j] > 0) {
-
inversion += 1;
-
}
-
}
-
}
-
// even number of "inversion" means solvable
-
if (1 === (inversion % 2)) {
-
return true;
-
}
-
return false;
-
};
btw. tested in FF 3.6, Chrome (Opera quit due to Date.now()) / Mac
8 2240 gits 5,390
Recognized Expert Moderator Expert
yes ... arrays and objects are passed 'by reference' so therefor a deep-cloning function like this will do a deep copy of a passed object ... when you have 'flat' objects/arrays, i mean one level of values and not objects ... then even a concat() would work to copy the array ... but when a member itself is an object then even this would pass a reference to the new object ...
kind regards
Dormilich 8,658
Recognized Expert Moderator Expert
thanks gits, I prototyped an array clone, that solves my problems. - Array.prototype.clone = function ()
-
{
-
var i, c = [];
-
for (i = this.length; i--;) {
-
if (i in this) {
-
c[i] = this[i];
-
}
-
}
-
return c;
-
};
gits 5,390
Recognized Expert Moderator Expert
yes ... for 'flat' arrays it will do :)
Dormilich 8,658
Recognized Expert Moderator Expert
I only use "flat" arrays in this case.
wanna try it? play here.
tested FF / Chrome / Opera / Safari – Mac, definitely not working in IE
Dormilich 8,658
Recognized Expert Moderator Expert @gits
that is, everything that is not a primitive?
gits 5,390
Recognized Expert Moderator Expert
... nearly ... functions are not 'primitives' but are even copied instead of referenced ... which is a bit confusing ...
Dormilich 8,658
Recognized Expert Moderator Expert
does that mean that, in case of event handlers via addEventListener(), the function body is copied to the element (or whereever the handler is stored)?
gits 5,390
Recognized Expert Moderator Expert
that seems a bit tricky/unclear ... in case you do: - var foo = function() {
-
alert('bar');
-
}
-
-
node.addEventListener('click', foo, true);
i think it passes a reference since we created one before ... when you then do: -
foo = function() {
-
alert('foobar');
-
}
-
-
othernode.addEventListener('click', foo, true);
the old ref is dropped but the first click-handler has the old foo-function while the othernode will have the new foo-function assigned. so in javascript it seems that basicly it doesn't really pass by reference but i seems more likely to pass a value of a reference or similar ... so that you could drop references like above but the old 'content' persists in an anonymous context then ... hopefully that makes any sense :) ... i think this seems like closures are supposed to work ...
an example of this behaviour is as follows: -
var a = {
-
foo : { test: 1, test1: 2 },
-
bar : function(a) { alert(a) },
-
foobar: 'foobar1',
-
arr : [1, [8, 9], {1: 1, 2: 2}, 4]
-
};
-
-
var b = {};
-
-
b.bar = a.bar;
-
b.arr = a.arr;
-
-
// alerts 2
-
a.bar(2);
-
-
b.arr[1] = ['foo'];
-
-
b.bar = function(a) { alert(a + a) };
-
-
// alerts 2
-
a.bar(2);
-
-
// logs the array ['foo'] even for obj a
-
console.log(a.arr)
-
here are the references dropped too - the function is 'new' in b but 'old' in a while the references in the b.arr-array persists and the new value 'foo' then is reflected in both arr-arrays of a and b
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: harry |
last post by:
I have 2 multi-dim arrays
double subTotals = null;
String rowTitles = null;
I want to pass them to a function that initialises & populates them like
so -
loadData( rowTitles, subTotals);
|
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Gent |
last post by:
I have two questions which are very similar:
Is it possible to return an object in C++. Below is part of my code
for reference however I am more concerned about the concept. It seems
like the...
|
by: Mike P |
last post by:
I will be passing my function a two dimensional array of varying length.
Within that array is one data point, and the number of times it should loop
through.
So, for example, I might pass this...
|
by: Kurt Richardson |
last post by:
Hi all
Sorry to bother you with what is probably a really
trivial question for you C++ experts. My programming
skill are pretty amateur, but I'm pretty good at VB.NET.
However, I'm wanting to...
| |
by: John Pass |
last post by:
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line...
|
by: Zytan |
last post by:
I know there are no pointers in C#, but if you do:
a = b;
and a and b are both arrays, they now both point to the same memory
(changing one changes the other). So, it makes them seem like...
|
by: Jack |
last post by:
If I don't specify "ref" in the argument list when passing an array to the
callee, I am passing the array (reference) by value. But this makes me
confused because it actually means a "reference" of...
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible...
|
by: jodleren |
last post by:
Hi!
I have a function, a part of my code which I can use as a function. It
will return 2 arrays, and I am wondering what way to do so. Both
arrays hold strings, there are no special keys.
1)...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
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,...
|
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: 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...
|
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,...
|
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...
|
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...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |