JavaScript Object Notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gh0st1@pacbell.net

    JavaScript Object Notation

    Is it possible to have something like the following so that 'three'
    can access 'one':

    var x = {
    one: 1,
    two: { three:this.one } // Or parent.one ?
    }

    If not (which I have nearly come to a conclusion on) what is the next
    best way to accomplish this?

  • RobG

    #2
    Re: JavaScript Object Notation

    On May 1, 8:36 am, gh0...@pacbell. net wrote:
    Is it possible to have something like the following so that 'three'
    can access 'one':
    >
    var x = {
    one: 1,
    two: { three:this.one } // Or parent.one ?
    Functions have a this keyword whose value is set by the calling
    function when the function is called, plain object don't have a this
    property. You can use:

    two: { three: x.one }


    --
    Rob

    Comment

    • -Lost

      #3
      Re: JavaScript Object Notation

      RobG wrote:
      On May 1, 8:36 am, gh0...@pacbell. net wrote:
      >Is it possible to have something like the following so that 'three'
      >can access 'one':
      >>
      >var x = {
      > one: 1,
      > two: { three:this.one } // Or parent.one ?
      >
      Functions have a this keyword whose value is set by the calling
      function when the function is called, plain object don't have a this
      property. You can use:
      >
      two: { three: x.one }
      Really? I always get "x has no properties."

      --
      -Lost
      Remove the extra words to reply by e-mail. Don't e-mail me. I am
      kidding. No I am not.

      Comment

      • RobG

        #4
        Re: JavaScript Object Notation

        On May 1, 12:30 pm, -Lost <maventheextraw o...@techie.com wrote:
        RobG wrote:
        On May 1, 8:36 am, gh0...@pacbell. net wrote:
        Is it possible to have something like the following so that 'three'
        can access 'one':
        >
        var x = {
        one: 1,
        two: { three:this.one } // Or parent.one ?
        >
        Functions have a this keyword whose value is set by the calling
        function when the function is called, plain object don't have a this
        property. You can use:
        >
        two: { three: x.one }
        >
        Really? I always get "x has no properties."
        I didn't say it worked... :-x

        I guess the closest is:

        var x = { one: 1 };
        x.two = { three: x.one};
        alert( x.two.three );


        --
        Rob

        Comment

        • -Lost

          #5
          Re: JavaScript Object Notation

          RobG wrote:
          On May 1, 12:30 pm, -Lost <maventheextraw o...@techie.com wrote:
          >RobG wrote:
          >>On May 1, 8:36 am, gh0...@pacbell. net wrote:
          >>>Is it possible to have something like the following so that 'three'
          >>>can access 'one':
          >>>var x = {
          >>> one: 1,
          >>> two: { three:this.one } // Or parent.one ?
          >>Functions have a this keyword whose value is set by the calling
          >>function when the function is called, plain object don't have a this
          >>property. You can use:
          >> two: { three: x.one }
          >Really? I always get "x has no properties."
          >
          I didn't say it worked... :-x
          Oh! Hehe. ;)
          I guess the closest is:
          >
          var x = { one: 1 };
          x.two = { three: x.one};
          alert( x.two.three );
          Thanks RobG.

          --
          -Lost
          Remove the extra words to reply by e-mail. Don't e-mail me. I am
          kidding. No I am not.

          Comment

          • aksus-69@yandex.ru

            #6
            Re: JavaScript Object Notation

            Gecko has two beautiful methods - getter and setter.

            var x = {
            one: 1,
            get two() {
            return this.one + 1;
            }
            }

            See http://developer.mozilla.org/en/docs...rs_and_Setters


            Comment

            Working...