inner classes proble

Discussion in 'Javascript' started by josh, Apr 18, 2008.

  1. josh

    josh Guest

    Hi, I've the following problem:

    If I have, for example,

    BB =
    {
    CC =
    {
    makeDecision : function()
    {
    return "ko";
    },

    b : isOk ? "ok" : makeDecision()
    }
    }

    now if I done


    BB.CC.b where isOK == false;

    than I have na error that makeDecision() is not defined

    now is not possible to assign a variable value calling a function?

    how can I make that?

    Thanks
     
    josh, Apr 18, 2008
    #1
    1. Advertising

  2. josh

    Tom de Neef Guest

    "josh" <>
    > If I have, for example,
    >
    > BB =
    > {
    > CC =
    > {
    > makeDecision : function()
    > {
    > return "ko";
    > },
    >
    > b : isOk ? "ok" : makeDecision()
    > }
    > }
    >
    > now if I done
    >
    >
    > BB.CC.b where isOK == false;
    >
    > than I have na error that makeDecision() is not defined
    >


    I am learning myself, so bear with me.
    This has to do with the scope chain. The scope chain is altered when you
    enter/leave a function. But you do not enter a function. You have an object
    BB with property CC which is an object with two properties: makeDecision and
    b. But 'entering' an object does not change the scope chain. BB.CC.b =
    makeDicision() will be looking for makeDecision in the global execution
    context. But it is not a property of the global variable and thus not
    defined.
    A solution would be to call BB.CC.makeDecision()

    Tom
     
    Tom de Neef, Apr 18, 2008
    #2
    1. Advertising

  3. josh <> writes:

    > Hi, I've the following problem:
    >
    > If I have, for example,
    >
    > BB =
    > {
    > CC =
    > {
    > makeDecision : function()
    > {
    > return "ko";
    > },
    >
    > b : isOk ? "ok" : makeDecision()
    > }
    > }
    >
    > now if I done
    >
    >
    > BB.CC.b where isOK == false;
    >
    > than I have na error that makeDecision() is not defined


    Yes, because there isn't one. You just made BB.CC.makeDecision. Assuming
    you meant BB = { C: { ... } } instead of BB = { C = { ... } }

    > now is not possible to assign a variable value calling a function?


    Sure it is, but you cannot refer to a property of an object literal
    within the object literal.

    IOW: you can't do:

    var o = {
    f: 2;
    g: o.f;
    }

    or anything similar.

    In your case, assuming you actually want makeDecision to be a property
    of BB.CC, you can do:


    var BB = { CC : { makeDecision: function() { return "ko" } } };

    BB.CC.b = isOk ? "ok" : makeDecision();


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Apr 18, 2008
    #3
  4. Joost Diepenmaat wrote:
    > In your case, assuming you actually want makeDecision to be a property
    > of BB.CC, you can do:
    >
    >
    > var BB = { CC : { makeDecision: function() { return "ko" } } };
    >
    > BB.CC.b = isOk ? "ok" : makeDecision();


    Probably you meant something along

    with (BB) CC.b = isOk ? "ok" : CC.makeDecision();

    because there would not be such a method in the scope chain.


    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, Apr 18, 2008
    #4
  5. Thomas 'PointedEars' Lahn <> writes:

    > Joost Diepenmaat wrote:
    >> In your case, assuming you actually want makeDecision to be a property
    >> of BB.CC, you can do:
    >>
    >>
    >> var BB = { CC : { makeDecision: function() { return "ko" } } };
    >>
    >> BB.CC.b = isOk ? "ok" : makeDecision();

    >
    > Probably you meant something along
    >
    > with (BB) CC.b = isOk ? "ok" : CC.makeDecision();
    >
    > because there would not be such a method in the scope chain.


    Erm, yeah that's what I meant. Although I wouldn't use with() in this
    case.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Apr 18, 2008
    #5
  6. Joost Diepenmaat wrote:
    > Thomas 'PointedEars' Lahn <> writes:
    >> Joost Diepenmaat wrote:
    >>> In your case, assuming you actually want makeDecision to be a property
    >>> of BB.CC, you can do:
    >>>
    >>> var BB = { CC : { makeDecision: function() { return "ko" } } };
    >>>
    >>> BB.CC.b = isOk ? "ok" : makeDecision();

    >> Probably you meant something along
    >>
    >> with (BB) CC.b = isOk ? "ok" : CC.makeDecision();
    >>
    >> because there would not be such a method in the scope chain.

    >
    > Erm, yeah that's what I meant. Although I wouldn't use with() in this
    > case.


    What better case is there? You know which properties the object has (so
    `isOk' could not be looked up there), and you use the reference twice in a
    statement.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Apr 18, 2008
    #6
  7. Thomas 'PointedEars' Lahn <> writes:

    > Joost Diepenmaat wrote:
    >> Erm, yeah that's what I meant. Although I wouldn't use with() in this
    >> case.

    >
    > What better case is there? You know which properties the object has (so
    > `isOk' could not be looked up there), and you use the reference twice in a
    > statement.


    It's mostly a matter of taste, but I'd probably use it only if I was
    dealing with a few (or a lot) more properties. The only place I've used
    with() at all recently was with some code that had generator functions
    for each HTML element (and I didn't want to make all of them global).

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Apr 18, 2008
    #7
    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.
Similar Threads
  1. lonelyplanet999
    Replies:
    1
    Views:
    2,226
    VisionSet
    Nov 13, 2003
  2. Razvan
    Replies:
    5
    Views:
    11,311
    Dale King
    Jul 27, 2004
  3. Christian Bongiorno
    Replies:
    5
    Views:
    547
    Chris Uppal
    Aug 30, 2004
  4. Carlo v. Dango
    Replies:
    14
    Views:
    1,038
    Alex Martelli
    Oct 19, 2003
  5. Pyenos
    Replies:
    2
    Views:
    389
    Pyenos
    Dec 27, 2006
Loading...

Share This Page