www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] Difference between object['element'] and object.element?

  1. #1
    Join Date
    Aug 2006
    Posts
    45

    resolved [RESOLVED] Difference between object['element'] and object.element?

    Hi all, long time no see! How are you doing?

    Anyway, here's my question: Is there any difference between object['element'] and object.element?

    Thanks a dozen!

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    They reference the same element, so the only difference is symantics.

    You can use a string, a var containing a string, or an integer within the braces to access an element.

    However, the other syntax is literal, and cannot be a var.

    Some may also make the comment that this is the "old" way of referencing things, but it still works.

  3. #3
    Join Date
    Aug 2006
    Posts
    45
    Thanks for your quick reply
    Which of the two is the 'old' way?

  4. #4
    Join Date
    Nov 2002
    Posts
    4,473
    (ROFL) both.

    The "new" way would involve DOM methods.

  5. #5
    Join Date
    Aug 2006
    Posts
    45
    Actually, I am not talking about referencing DOM objects, but about defining arrays and defining custom objects (for object oriented scripts). I know how to use DOM methods for DOM HTML objects.

    But what about custom Javascript objects? What would you say - which the two is better?

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by pilau View Post
    But what about custom Javascript objects? What would you say - which the two is better?
    that like asking "what's better; a car or a chainsaw?"...

    for hand-coded source, ob.prop is quicker and more readable.
    for machine-generated code, op["prop"] syntax allows spaces, reserved names like ob['class'], var-refs ( ob[stringVar] ), and expressions (ob['pr'+'op'])

  7. #7
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by rnd me View Post
    that like asking "what's better; a car or a chainsaw?"...
    Well consider this, in this case I didn't really know the difference between the car and the chainsaw...

    Another question - if I declare an object using one way can I call it's elements using the other?

    Also, is one way prefered for custom arrays defined through the Array class and the other for objects defined through custom classes?

    Thanks!

  8. #8
    Join Date
    Sep 2009
    Location
    Kiel, Germany
    Posts
    21
    Javascript Guru Douglas Crockford says, you should prefer the dot-notation. The other notation requires a little more work on the js-engines as there is always a new string to handle with myObject["something"].

    So, there is only a need for the myObject["something"]-notation if you are going some dynamic way like something = "somewhere"; myObject[something];

    Greetings from Germany,
    Kim

    --
    www.youngvisions.net | www.deutscherfussballblog.de | www.schneider.ws

  9. #9
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    ["xxx"] is called array syntax, but it works just as well for objects.

    i don't think there's any speed penatly for using ["xxx"] vs .xxx, the scriptBdy is aready parsed by the time ['xxx'] or .xxx would be assessed. most benchmarks i've seen fail to report any diff, and when they do, it's less than %1...

    there is no short-hand of what goes where; you'll have to develop the judgment to decide that for yourself on a case-by-case basis.

    will non-special names and hand-coded routines, i would use .xxx first; it's more readable.

  10. #10
    Join Date
    Aug 2006
    Posts
    45
    Thanks a lot guys
    Now I understand this a lot better - on the whole, there isn't any important difference between the two. I was always using the dot method, anyway

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles