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

array declaration on FF vs IE

hi,

There is something that I don't understand well, I use array for
combining strings like

items = ['hello world', ', how are you', '?', 'the end'].join('');

in FF this works well but in IE I had to add something like this

var items = new Array();
items = ['hello world', ', how are you', '?', 'the end'].join('');

My question is why is there a need for var items = new Array(): and
why items = new Array(); doesn't work? I don't want the items to be a
global variable, since it was declare inside a function I want it to
be local only

Thanks
james

Sep 13 '07 #1
4 2795
james_027 said the following on 9/12/2007 9:29 PM:
hi,

There is something that I don't understand well, I use array for
combining strings like

items = ['hello world', ', how are you', '?', 'the end'].join('');

in FF this works well but in IE I had to add something like this

var items = new Array();
items = ['hello world', ', how are you', '?', 'the end'].join('');
In what IE? This code:

items = ['hello world', ', how are you', '?', 'the end'].join('');
alert(items)

alerts what I would expect: "hello world, how are you? the end". Which
is the same exact results Firefox gives. Even typeof items gives
"string" in both.
My question is why is there a need for var items = new Array():
There isn't.
and why items = new Array(); doesn't work?
It depends on *all* of your code and how it is "not working".

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 13 '07 #2
james_027 meinte:
if the var invoice_items = new Array(); is commented it won't work in
IE6 saying "the object doesn't support this property or method" but
when it is uncommented it works.
In this case invoice_item becomes a *global* variable. var invoice_items
= [...] will work.

Gregor
--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 13 '07 #3
On Sep 13, 8:36 am, Gregor Kofler wrote:
james_027 meinte:
if the var invoice_items = new Array(); is commented it won't
work in IE6 saying "the object doesn't support this property
or method" but when it is uncommented it works.

In this case invoice_item becomes a *global* variable.
var invoice_items = [...] will work.
To which it would be possible to add an explanation of why that might
make a difference on IE and not necessarily matter on other browsers.
It will be because the HTML contains an element with an ID attribute
set to "invoice_item" and when that is the case IE adds a reference to
the DOM element as a named property of the global object under the
name "invoice_item" and makes that property read only. Thus the array
does not get assigned to the property of the global object when -
invoice_items = [...]; - is executed, and subsequent attempts to call
array methods on an object that remains an Element result in the
(accurate) error "object doesn't support this property or method".

There are two ways of avoiding this issue. The first is, as suggested,
declaring the - invoice_items - in the functions so it masks the
property of the global object. The other is to explicitly declare -
invoice_items - as a global variable. If that is done IE does not use
the existence of an element in the DOM with a corresponding ID
attribute as an excuse for creating a property of the global object,
and so the resulting declared global variable has no DOM Element
reference assigned and it remains read/write.

The whole issue is avoided by following the 'best practice' advice of
always explicitly declaring all variables.

Sep 13 '07 #4
Henry meinte:

[intersting explanation snipped]
The whole issue is avoided by following the 'best practice' advice of
always explicitly declaring all variables.
That's perhaps the reason, why I've never encountered this problem.
Anyway, interesting to learn about another absurdity of the IE JS and
DOM implementation.
Gregor
--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 13 '07 #5

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

Similar topics

8
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101,...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
19
by: DarelRex | last post by:
Is it possible to pass a 2-D, statically defined array? Here's a 1-D example that won't work: void foo() { int myArray ; bar(myArray); } void bar(int *arr) {
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
18
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
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,...
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.