Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 24th, 2005, 02:13 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object literal syntax

I am familiar with this type of literal object syntax:
Code:
var myObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};
as well as this, for arrays:
Code:
slides = [
    {url: 'slide0001.html', title: 'Looking Ahead'},
    {url: 'slide0008.html', title: 'Forecast'},
    {url: 'slide0021.html', title: 'Summary'}
];
but the following is new to me:
Code:
obj[‘on’ + event]
What does this mean? obj is an object, I think.

Thanks,
Tim

 
Old May 25th, 2005, 12:14 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Update:

So I've figured out that obj["on" + event] is an object, and that it's object property syntax (e.g. obj["property"]).

What is still confusing me is the + event part. Does the "on" + event just resolve to a string (given that event would be a reference to another object)?

 
Old May 27th, 2005, 03:07 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Yes, it does resolve to a string because anything plus a string equals a string. Of course, what that final value is will depend on the value of the event variable.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old May 28th, 2005, 11:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

To clarify:
Code:
obj[‘on’ + event]
if event = "click" then
 the above equals:
Code:
obj["onclick"]
which is the same as:
Code:
obj.onclick
Presumably the author was tring to dynamically assign event handlers.


--

Joe (Microsoft MVP - XML)
 
Old May 28th, 2005, 07:44 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

@ Joe & Nicholas: Thanks.

@ Joe: yes, I can see now that that's exactly what the author was doing.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Label vs Literal pinch BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 9th, 2007 01:01 PM
Object reference Syntax help lidanny General .NET 5 September 3rd, 2006 07:17 AM
Fields object syntax in VBA query stepdev Access VBA 11 March 3rd, 2006 04:53 PM
Using right() to return a literal value Somed00d Access 4 February 8th, 2006 02:56 PM
Syntax for OleDb Command Object Parameters aadz5 ASP.NET 1.0 and 1.1 Basics 4 November 20th, 2003 03:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.