json in array -> overlap problem

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

    json in array -> overlap problem

    hi to all!

    i have the following code (i stripped down the code to a short
    example, but still it does reproduce the problem):

    var objs = new Array();
    obj = {
    parameter : " "
    }

    objs[0] = obj;
    objs[0].parameter = "apple";

    objs[1] = obj;
    objs[1].parameter = "banana";

    alert(objs[0].parameter);

    i create a json object (obj) and insert into 2 items of a previously
    created array (objs[0] and objs[1]). inspecting the array, i expect
    objs[0].parameter to be "apple", not "banana", which is what i keen on
    getting.
    any idea?
    tia, gustavo

  • d d

    #2
    Re: json in array -> overlap problem

    pistacchio wrote:
    i create a json object (obj) and insert into 2 items of a previously
    created array (objs[0] and objs[1]). inspecting the array, i expect
    objs[0].parameter to be "apple", not "banana", which is what i keen on
    getting.
    objs[0] and objs[1] are both pointers to obj. You're not creating new
    instances of obj, you're creating new references to the obj object.

    Define obj like this:

    obj=function(){ this.parameter= "";}

    and in your array, do this:

    objs[0] = new obj();
    objs[0].parameter = "apple";

    objs[1] = new obj();
    objs[1].parameter = "banana";

    ~dd

    Comment

    • Douglas Crockford

      #3
      Re: json in array -> overlap problem

      pistacchio wrote:
      hi to all!
      >
      i have the following code (i stripped down the code to a short
      example, but still it does reproduce the problem):
      >
      var objs = new Array();
      obj = {
      parameter : " "
      }
      >
      objs[0] = obj;
      objs[0].parameter = "apple";
      >
      objs[1] = obj;
      objs[1].parameter = "banana";
      >
      alert(objs[0].parameter);
      >
      i create a json object (obj) and insert into 2 items of a previously
      created array (objs[0] and objs[1]). inspecting the array, i expect
      objs[0].parameter to be "apple", not "banana", which is what i keen on
      getting.
      any idea?
      var objs = [
      {parameter: 'apple'},
      {parameter: 'banana'}
      ];

      Comment

      • pistacchio

        #4
        Re: json in array -> overlap problem

        d d ha scritto:
        pistacchio wrote:
        >i create a json object (obj) and insert into 2 items of a previously
        >created array (objs[0] and objs[1]). inspecting the array, i expect
        >objs[0].parameter to be "apple", not "banana", which is what i keen on
        >getting.
        >
        objs[0] and objs[1] are both pointers to obj. You're not creating new
        instances of obj, you're creating new references to the obj object.
        >
        Define obj like this:
        >
        obj=function(){ this.parameter= "";}
        >
        and in your array, do this:
        >
        objs[0] = new obj();
        objs[0].parameter = "apple";
        >
        objs[1] = new obj();
        objs[1].parameter = "banana";
        >
        ~dd
        hmmm.. so a js array can store 'objectified functions' but not json objects?

        Comment

        • -Lost

          #5
          Re: json in array -> overlap problem

          pistacchio wrote:
          hmmm.. so a js array can store 'objectified functions' but not json
          objects?
          A JavaScript array can hold anything. Boolean values, Objects,
          Functions, numeric and string primitives, et cetera.

          I don't believe there is a limit to what an array can hold, but I could
          be wrong.

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

          Comment

          Working...