Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript function name, duplicates, good practices

 
Jesus Angeles
Ranch Hand
Posts: 2068
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man Bear, I guess I should have stopped playing my game when I hit the reply button!

Eric
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, nothing new. But little has been written on using object-oriented Javascript.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic