Explicit declaration of array type required?

Discussion in 'Javascript' started by d.adamkiewicz@i7.com.pl, Aug 6, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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>
     
    Thomas 'PointedEars' Lahn, Aug 6, 2007
    #2
    1. Advertising

  3. jim Guest

    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
     
    jim, Aug 6, 2007
    #3
  4. Thomas 'PointedEars' Lahn wrote:
    > 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>
     
    Thomas 'PointedEars' Lahn, Aug 6, 2007
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Aug 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page