File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes javascript function name, duplicates, good practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript function name, duplicates, good practices" Watch "javascript function name, duplicates, good practices" New topic
Author

javascript function name, duplicates, good practices

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
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: 60732
    
  65

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:



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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: 15376
    
    6
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: 2049
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: 60732
    
  65

No, nothing new. But little has been written on using object-oriented Javascript.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javascript function name, duplicates, good practices
 
Similar Threads
Servlet with Javascript
CSS: is it possible to combine relative and absolute heights?
issue in javascript
Federated UI's
is it possible to have a pop up button in my jsp wrapper without js?