Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Explicit declaration of array type required?

Reply
Thread Tools

Explicit declaration of array type required?

 
 
d.adamkiewicz@i7.com.pl
Guest
Posts: n/a
 
      08-06-2007
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

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>
 
Reply With Quote
 
 
 
 
jim
Guest
Posts: n/a
 
      08-06-2007

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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-06-2007
Thomas 'PointedEars' Lahn wrote:
> (E-Mail Removed) 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>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-06-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type declaration in declaration of a parameter or return type of a function Luca Forlizzi C Programming 4 11-14-2010 09:30 PM
Explicit specialization of member template when type is anothertemplate type. jason.cipriani@gmail.com C++ 5 12-05-2008 05:33 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments