again.. objects (quite lame, i suppose..)

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

    #1

    again.. objects (quite lame, i suppose..)

    hi to all again!
    well, if i have the simplest object:

    a=new Object()

    it seems i can't assign properties to it..

    b.c="something"

    as when i say:

    writeln(b.c)

    it return an error.. any suggestion?
  • Martin Honnen

    #2
    Re: again.. objects (quite lame, i suppose..)



    Gu wrote:

    [color=blue]
    > well, if i have the simplest object:
    >
    > a=new Object()
    >
    > it seems i can't assign properties to it..
    >
    > b.c="something"[/color]

    Above you create a variable named a now you try to set a property on b
    that can't work.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Gu

      #3
      Re: again.. objects (quite lame, i suppose..)

      On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <mahotrash@yaho o.de>
      wrote:
      [color=blue]
      >
      >
      >Gu wrote:
      >
      >[color=green]
      >> well, if i have the simplest object:
      >>
      >> a=new Object()
      >>
      >> it seems i can't assign properties to it..
      >>
      >> b.c="something"[/color]
      >
      >Above you create a variable named a now you try to set a property on b
      >that can't work.[/color]

      just a mistyping, of course the code is:

      a=new Object();
      a.b="something" ;

      Comment

      • Gu

        #4
        Re: again.. objects (quite lame, i suppose..)

        anyway, i partially solved the problem (with the previous post i din't
        mean that the problem was the mistyping but.. oh, well, nevermind).
        where i'm aiming is a way to "dynamicall y" act on objects.
        say i have the object kitchen created upon the object room. i take an
        imput from the user asking him which room he would like to know the
        description of:

        function room(){
        this.descriptio n="blabla";
        }

        kitchen=new room();
        kitchen.descrip tion="blablabla 2";

        (don't mind eventual typo errors, i'm not copy pasting it and the
        problem is a "conceptual " one). now, the inprut from the user (or from
        a function, this is not the point) is kitchen. i could do this:

        document.writel n(eval(userInpu t+".description "));
        [where userInput is "kitchen"]
        this actually work so far. but:
        1 is there a way to avoid using eval?
        2 what if the input is, say, "bathroom" which is not set yet? how can
        i check if given object exists?

        thanks in advance

        Comment

        • Lee

          #5
          Re: again.. objects (quite lame, i suppose..)

          Gu said:[color=blue]
          >
          >anyway, i partially solved the problem (with the previous post i din't
          >mean that the problem was the mistyping but.. oh, well, nevermind).
          >where i'm aiming is a way to "dynamicall y" act on objects.
          >say i have the object kitchen created upon the object room. i take an
          >imput from the user asking him which room he would like to know the
          >description of:
          >
          >function room(){
          > this.descriptio n="blabla";
          >}
          >
          >kitchen=new room();
          >kitchen.descri ption="blablabl a2";
          >
          >(don't mind eventual typo errors, i'm not copy pasting it and the
          >problem is a "conceptual " one). now, the inprut from the user (or from
          >a function, this is not the point) is kitchen. i could do this:
          >
          >document.write ln(eval(userInp ut+".descriptio n"));
          >[where userInput is "kitchen"]
          >this actually work so far. but:
          >1 is there a way to avoid using eval?
          >2 what if the input is, say, "bathroom" which is not set yet? how can
          >i check if given object exists?[/color]

          Any time you're going to post code and say that it doesn't work,
          copy and paste it from code that you've tested, or you just waste
          everybody's time.

          Here are some examples of how you can set and get attributes of
          a custom object:

          <html>
          <body>
          <script>
          function Room(name,width ,length) {
          this.name=name;
          this.width=widt h;
          this.length=len gth;
          }
          var alpha=new Room("Dining",1 2,18);
          alpha.color="bu rgundy";
          alpha["flooring"]="hardwood";

          alert(alpha.nam e);
          var dimension="widt h";
          alert(alpha[dimension]);
          if(alpha.color) {
          alert(alpha.col or);
          }
          var str="";
          for (attr in alpha) {
          str += attr + ": \"" + alpha[attr] + "\"\n";
          }
          alert(str);
          </script>
          </body>
          </html>

          Comment

          • Gu

            #6
            Re: again.. objects (quite lame, i suppose..)

            On 14 May 2005 10:04:04 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:
            [color=blue]
            >Gu said:[color=green]
            >>
            >>anyway, i partially solved the problem (with the previous post i din't
            >>mean that the problem was the mistyping but.. oh, well, nevermind).
            >>where i'm aiming is a way to "dynamicall y" act on objects.
            >>say i have the object kitchen created upon the object room. i take an
            >>imput from the user asking him which room he would like to know the
            >>description of:
            >>
            >>function room(){
            >> this.descriptio n="blabla";
            >>}
            >>
            >>kitchen=new room();
            >>kitchen.descr iption="blablab la2";
            >>
            >>(don't mind eventual typo errors, i'm not copy pasting it and the
            >>problem is a "conceptual " one). now, the inprut from the user (or from
            >>a function, this is not the point) is kitchen. i could do this:
            >>
            >>document.writ eln(eval(userIn put+".descripti on"));
            >>[where userInput is "kitchen"]
            >>this actually work so far. but:
            >>1 is there a way to avoid using eval?
            >>2 what if the input is, say, "bathroom" which is not set yet? how can
            >>i check if given object exists?[/color]
            >
            >Any time you're going to post code and say that it doesn't work,
            >copy and paste it from code that you've tested, or you just waste
            >everybody's time.
            >
            >Here are some examples of how you can set and get attributes of
            >a custom object:
            >
            ><html>
            ><body>
            ><script>
            > function Room(name,width ,length) {
            > this.name=name;
            > this.width=widt h;
            > this.length=len gth;
            > }
            > var alpha=new Room("Dining",1 2,18);
            > alpha.color="bu rgundy";
            > alpha["flooring"]="hardwood";
            >
            > alert(alpha.nam e);
            > var dimension="widt h";
            > alert(alpha[dimension]);
            > if(alpha.color) {
            > alert(alpha.col or);
            > }
            > var str="";
            > for (attr in alpha) {
            > str += attr + ": \"" + alpha[attr] + "\"\n";
            > }
            > alert(str);
            ></script>
            ></body>
            ></html>[/color]

            hi lee, thanks for your reply. your codes are useful, but the suppose
            i know i' m working on "alpha", while i really don't know if the user
            tells javascript that the room he wants info about is "alpha" and not
            "beta"
            thnaks

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: again.. objects (quite lame, i suppose..)

              Gu wrote:
              [color=blue]
              > function room(){
              > this.descriptio n="blabla";
              > }
              >
              > kitchen=new room();
              > kitchen.descrip tion="blablabla 2";[/color]

              I'd rather use

              function Room(sDescr)
              {
              this.descriptio n = sDescr || "blabla";
              }

              var kitchen = new Room("blablabla 2");
              [color=blue]
              > (don't mind eventual typo errors, i'm not copy pasting it and the
              > problem is a "conceptual " one).[/color]

              Even wrong or misunderstood concepts can hardly be recognized by outsiders
              when you post the wrong code.

              Especially, you posted the code that does not work but you did not explain
              where and how exactly it does not work. The "typos" aside, your code *up
              to here* appears to be syntactically and conceptually OK.
              [color=blue]
              > now, the inprut from the user (or from
              > a function, this is not the point) is kitchen. i could do this:
              >
              > document.writel n(eval(userInpu t+".description "));[/color]

              Eeek.
              [color=blue]
              > [where userInput is "kitchen"]
              > this actually work so far. but:
              > 1 is there a way to avoid using eval?[/color]

              Yes, and it is explained in the FAQ as well as in countless postings.
              [color=blue]
              > 2 what if the input is, say, "bathroom" which is not set yet? how can
              > i check if given object exists?[/color]

              Obviously you did not use any (group) search engine prior.


              PointedEars
              --
              The German psychs, the German authorities, the German secret service agents
              are [...] fanatics, they are insane and known of persecuting innocent people
              and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology ,
              <16d1deb5.04022 60945.4d4316af@ posting.google. com>

              Comment

              • Grant Wagner

                #8
                Re: again.. objects (quite lame, i suppose..)

                "Gu" <pistacchio@gma il.com> wrote in message
                news:b98c81pfdj 1jiqtl30jm8ssn0 h6033mu4m@4ax.c om...[color=blue]
                > On Sat, 14 May 2005 17:12:48 +0200, Martin Honnen <mahotrash@yaho o.de>
                > wrote:
                >[color=green]
                >>
                >>
                >>Gu wrote:
                >>
                >>[color=darkred]
                >>> well, if i have the simplest object:
                >>>
                >>> a=new Object()
                >>>
                >>> it seems i can't assign properties to it..
                >>>
                >>> b.c="something"[/color]
                >>
                >>Above you create a variable named a now you try to set a property on b
                >>that can't work.[/color]
                >
                > just a mistyping, of course the code is:
                >
                > a=new Object();
                > a.b="something" ;[/color]

                var a = new Object(); // or {};
                a.b = "something" ;
                document.writel n(a.b);

                Tested and working in IE 6.0.2900, Firefox 1.0.4, Netscape 4.8, Opera
                8.00, Mozilla 1.7.8. Quite frankly, it should work in any Web browser
                that supports a default HTMLDocument object called document and
                ECMA-262.

                --
                Grant Wagner <gwagner@agrico reunited.com>
                comp.lang.javas cript FAQ - http://jibbering.com/faq


                Comment

                Working...