473,399 Members | 3,401 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,399 software developers and data experts.

Explicit declaration of array type required?

Hello Folks

I was wondering if the following could be better written:

var myObject = {};
/*
in this case explicit declaration seems to be mandatory
otherwise 'myObject.ala has no properties' error occurs
*/
if (typeof myObject["ala"] == "undefined") {
myObject["ala"] = [];
}

myObject["ala"].push("ela");

Regards
Darek

Aug 6 '07 #1
5 2990
d.***********@i7.com.pl wrote:
I was wondering if the following could be better written:

var myObject = {};
/*
in this case explicit declaration seems to be mandatory
otherwise 'myObject.ala has no properties' error occurs
*/
Of course, `undefined' has no properties.
if (typeof myObject["ala"] == "undefined") {
myObject["ala"] = [];
}
This condition is only necessary if the "ala" property is running the risk
of not being defined or explicitly assigned `undefined' before.
myObject["ala"].push("ela");
Backwards compatible:

var myObject = new Object();
myObject.ala = new Array("ela");

or (ECMAScript 3 compliant):

var myObject = {ala: ["ela"]};
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 6 '07 #2
jim

var myObject = {ala: []};
myObject.ala.push("ela");

could also do:

window.myObject={
ala:[],
alb:{a:1,b:"two",c:[3]},
alc: (false)? true:false,
ald: "hello"
};

cheers, Jim

Aug 6 '07 #3
Thomas 'PointedEars' Lahn wrote:
d.***********@i7.com.pl wrote:
>if (typeof myObject["ala"] == "undefined") {
myObject["ala"] = [];
}

This condition is only necessary if the "ala" property is running the risk
of not being defined or explicitly assigned `undefined' before.
Clarification: It is not necessary at all, unless a specific program
behavior is attached to its having the value `undefined'.

myObject["ala"] = [];

creates the property and/or assigns a (new) value to it.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 6 '07 #4
jim wrote:
var myObject = {ala: []};
^^^^^^^^^^^^^^
myObject.ala.push("ela");

could also do:

window.myObject={
^^^^^^^^^^^^^^^^
ala:[],
alb:{a:1,b:"two",c:[3]},
alc: (false)? true:false,
ald: "hello"
};
Those two assignments are in no way equivalent.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 6 '07 #5
Thomas 'PointedEars' Lahn said the following on 8/6/2007 4:01 AM:
jim wrote:
>var myObject = {ala: []};
^^^^^^^^^^^^^^
>myObject.ala.push("ela");

could also do:

window.myObject={
^^^^^^^^^^^^^^^^
>ala:[],
alb:{a:1,b:"two",c:[3]},
alc: (false)? true:false,
ald: "hello"
};

Those two assignments are in no way equivalent.
And the phrase "could also do" indicates it as an alternative, not as an
equivalent.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 6 '07 #6

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

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
11
by: Tim | last post by:
Why won't the declaration of a struct array work if I do it like this: 1 typedef struct 2 { 3 int pens; 4 int pencils; 5 } Stationers; 6 .........
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
7
by: Jacob Schmidt | last post by:
Could anyone correct the error in my logic here?: #include <stdio.h> #include <stdlib.h> main () { const char message1 = {"\nCurved portion of graph -- D.G.A.C.\ \n A B C"}; const char...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
11
by: Martin Eisenberg | last post by:
Hi Antoine, just redirecting you... Antoine Trux wrote: > Hi, > > Is the following code legal: > > ------> code starts here <------ > #include <stddef.h>
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
2
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
10
by: Raman | last post by:
Hi All, Is it valid: struct test{ }; I mean, Can we have a struct containing no members? Is this a an
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
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 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.