If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

Variables defined inline are by reference and not by value

RESOLVED INVALID

Status

()

Core
JavaScript Engine
--
minor
RESOLVED INVALID
11 years ago
11 years ago

People

(Reporter: Chris, Unassigned)

Tracking

1.8 Branch
x86
Windows XP
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

11 years ago
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

Updated

11 years ago
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general

Updated

11 years ago
Version: unspecified → 1.8 Branch

Comment 1

11 years ago
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
Last Resolved: 11 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.