| Author |
javascript function name, duplicates, good practices
|
Jesus Angeles
Ranch Hand
Joined: Feb 26, 2005
Posts: 2038
|
|
There is no concept for namespaces, e.g. java packages, in javascript, if I am not wrong. Is there any good practices that we can apply to lower the possibility of same function names? This will come up a lot once we start having big number of javascript codes, and/or use third-party libraries.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56549
|
|
It's easy to create your own namespaces. Functions are first-class objects in Javascript, so they can be created as literals and assigned as properties of other objects. This is the basis of object-oriented Javascript. In the above example, I created a "namespace object" named Bear, and then created a function via a function literal and assigned it to the property named someFunction on Bear. To call the function:
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15362
|
|
Use OO JavaScript. Define an object specific to what you are doing and use that as a "Namespace" http://java.sun.com/javascript/ajaxinaction/Ajax_in_Action_ApB.html Eric
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15362
|
|
Man Bear, I guess I should have stopped playing my game when I hit the reply button! Eric
|
 |
Jesus Angeles
Ranch Hand
Joined: Feb 26, 2005
Posts: 2038
|
|
Originally posted by Bear Bibeault: It's easy to create your own namespaces. Functions are first-class objects in Javascript, so they can be created as literals and assigned as properties of other objects. This is the basis of object-oriented Javascript. In the above example, I created a "namespace object" named Bear, and then created a function via a function literal and assigned it to the property named someFunction on Bear. To call the function:
Thanks. Thats an interesting construct. Is this a new feature? I just read a 2006 beginner's book on javascript and it doesnt mention it. I see that scriptaculous uses it. [ March 27, 2007: Message edited by: Jesus Angeles ]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56549
|
|
|
No, nothing new. But little has been written on using object-oriented Javascript.
|
 |
 |
|
|
subject: javascript function name, duplicates, good practices
|
|
|