Closed Bug 378555 Opened 17 years ago Closed 17 years ago

Variables defined inline are by reference and not by value

Categories

(Core :: JavaScript Engine, defect)

1.8 Branch
x86
Windows XP
defect
Not set
minor

Tracking

()

RESOLVED INVALID

People

(Reporter: Chris, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3

This is a JavaScript issue.

When you declare more than one variable inline it will define those variables by reference to each other and not by value. So if you define two or more variables like so:

var _arr1 = _arr2 = [];

... and doing any array functions to either variable will cause some strange effects on each other.

Reproducible: Always

Steps to Reproduce:
1. <script type="text/JavaScript">
	var _i = 0;
	var _arr1 = _arr2 = [];
	for (_i=0;_i<navigator.plugins.length;_i++) {
		_arr1[_i] = navigator.plugins[_i].name;
	}
	_arr1 = _arr1.join("|||");

	alert(_arr2.toString());
</script>
2. Run Script
Actual Results:  
The script alerts a bunch of browser plug-ins

Expected Results:  
_arr2 should = null

I have experienced this for a while [maybe a year or so] throughout 1.x to 2.x
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → 1.8 Branch
This is not a bug; if you want a deep copy of an array, you must do so explicitly.  The same is true of objects, as well, and this behavior does not occur only inline, but also in functions.  JavaScript favors references over deep copies for performance reasons.
Status: UNCONFIRMED → RESOLVED
Closed: 17 years ago
Resolution: --- → INVALID
Chris: you are not declaring _arr2 as a var, you are just assigning to it in the initializer for var _arr1. Also, you must mean "_arr2 should = []", not "...null".

/be
You need to log in before you can comment on or make changes to this bug.