inner classes proble

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • josh

    inner classes proble

    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




  • Tom de Neef

    #2
    Re: inner classes proble

    "josh" <xdevel1999@gma il.com>
    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.makeDecis ion()

    Tom


    Comment

    • Joost Diepenmaat

      #3
      Re: inner classes proble

      josh <xdevel1999@gma il.comwrites:
      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.makeDecis ion. 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/

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: inner classes proble

        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>

        Comment

        • Joost Diepenmaat

          #5
          Re: inner classes proble

          Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
          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/

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: inner classes proble

            Joost Diepenmaat wrote:
            Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
            >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

            Comment

            • Joost Diepenmaat

              #7
              Re: inner classes proble

              Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
              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/

              Comment

              Working...