Archive for the ‘Uncategorized’ Category.

Creating namespaces with self-invoking functions

When a project’s javascript grows, it can be easy to lose ourselves, buried under thousands of lines of js code. That’s why I like to create namespaces to keep everything together.

Let’s say I have functions to work with cats and I create a namespace for it because there are lots of other functions related to dogs.

As you see, to access the cat list I must use the complete name including the namespaces like this : Animal.Cat.list. You can guess that it will get pretty long as the code grows.

Let’s make this call shorter.

Now, I only have to write c.list because c is an alias of the Animal.Cat namespace. Thanks to self-invocation!

You probably noticed that the code is longer but this is just an example. In a real situation, this technique would make your code much clearer.

Don’t forget to subscribe to my newsletter!

Announcing the jsKata libraries

I’m officially announcing the jsKata librairies.

Why?

I just wanted to write some public code and share it with others. Javascript has a lot of pains in the butt and jQuery is not the answer to every problem. jsKata is the answer to some of the problems that I face everyday while writing javascript.

A manifesto?

I wrote a manifesto because it helps me focusing on what and how I want to achieve with jsKata. This is the manifesto of version 0.2.

  • No internal dependence : every library can be used independently “as is”.
  • No external dependence : don’t depend on external libraries.
  • Everything is public : you know what you’re doing
  • Avoid objects : use closures
  • No unnecessary validation : if something goes wrong, an error will pop
  • No error catching : if an error pop, it goes all the way up
  • No DOM : jQuery already exists
  • No plugins : if a developer wants to add something, he will find a way around
  • Write good documentation : document as I code
  • Promote : a good library is nothing without users

What are the libraries available?

I don’t consider jsKata to be a library but more a set of libraries because each one can be used independently (see #1 in manifesto). For the moment, there are two librairies.

Undo & redo
I wrote about undo and undo & redo before. I took the code and put it in jsKata. You can look at the code or try the demo.

No freeze
This librairy is to avoid unresponsive script warning when you have a really long loop. It cuts a loop to create digestible chunks without a warning. I’ll write more about this one in the future but meanwhile, you can look at the code or try the demo.

GitHub

All the code is hosted on GitHub. I hope you will enjoy it!