Closed Thread Icon

Topic awaiting preservation: A quextion I think about JS and variable scope (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=26374" title="Pages that link to Topic awaiting preservation: A quextion I think about JS and variable scope (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: A quextion I think about JS and variable scope <span class="small">(Page 1 of 1)</span>\

 
Moobie
Bipolar (III) Inmate

From: UK
Insane since: Jul 2005

posted posted 08-03-2005 12:04

I'm just getting in to playing around with OO Javascript and having a problem, I think to do with scope, that I don't understand.

I have something similar to the code below that processes an array of data. Part of this processing involves finding the maximum and minimum values, but the code I have written to do this reverses the order of the 'data' array as though my maximum and minimum functions are operating on 'data' array itself rather than a local copy within these functions.

Can someone explain why?


code:
function dataMunch(data) {
	this.compareNumbers = function(a, b) {
		return a - b;
	}
	this.aMax = function(arr) {
		arr.sort(this.compareNumbers);
		return arr[arr.length - 1];
	}
	this.aMin = function(arr) {
		arr.sort(this.compareNumbers);
		return arr[0];
	}
	this.munchData = function(data) {
		var nMax = this.aMax(data);
		var nMin = this.aMin(data);
	}
	alert(data);
	this.munchData(data);
	alert(data);
}



Thanks!

(Edited by Moobie on 08-03-2005 12:42)

InI
Maniac (V) Inmate

From:
Insane since: Mar 2001

posted posted 08-03-2005 12:48

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

_Mauro
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

posted posted 08-03-2005 13:30

(that bloody InI account..) I was saying: in js, variables are passed by reference.

So when you call function(myArray), you really are passing a handle to myArray, rather than a copy.
In order to avoid affecting the original array, you should make a copy of it inside your function.

Moobie
Bipolar (III) Inmate

From: UK
Insane since: Jul 2005

posted posted 08-03-2005 13:49

That makes things much clearer, thanks for the explanation _Mauro!

(Edited by Moobie on 08-03-2005 14:33)

InI
Maniac (V) Inmate

From:
Insane since: Mar 2001

posted posted 08-03-2005 13:53

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

_Mauro
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

posted posted 08-03-2005 13:54

You're welcome... sigh, stuck between a trailing underscore and an account that can't post.
"Carefull what you wish.."

« BackwardsOnwards »

Show Forum Drop Down Menu