JavaScript language advanced tips and tricks

Mon, 06/30/2008 - 21:12 — John Riche

The guys behind the jslibs project have created a nice wiki with great JavaScript tips and tricks. Just see for yourself through the following samples. Some of them might not be available to all browsers but are worth a closer look...

Check if an object is not empty:

function isNotEmpty(obj){

for(var tmp in obj )

returntrue

}

Transform the arguments object into an array:

function foo(){

Array.slice(arguments);// is ['aa',11]

}

foo('aa',11);

The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system:

Comments

Hi John Riche,
I do not know why I am here, probably because of your chose title ...

There is nothing truly advanced in your suggestions, specially because most of these are wrong.

The isNotEmpty fails with every kind of "advanced instance"

function MyConstructor(){};

MyConstructor.prototype.myGenericMethod=function(){

};

isNotEmpty(new MyConstructor);// true, method failure

The singleton uses privileged methods without a reason, so as basic example is not that good.
However, the point is that a singleton pattern with a public instance, sounds really everything but advanced.

There are different way to implement a better singleton, and this is only one of them:

Singleton =function(){

function Singleton(){

if(_instance)

return _instance;

elseif(thisinstanceof arguments.callee){

_instance =this;

/** your constructor stuff here */

}

else

returnnew arguments.callee;

};

// you can use this scope to create prototypes too, or the external one

var _instance;

return Singleton;

}();

Singleton()=== Singleton()&&new Singleton()===new Singleton();

// true

With the function foo, and default arguments, you will never be able to set an empty string, a null, an integer (0), a boolean (false), because of your implementation. Check with === undefined instead.

Last creation works, but it is using a public static Array method to perform a simple task like this one:

var a =[1,2,3,4,5,6],

b =['a','b','c'];

a = a.splice(0,5).concat(b, a);

I do like people that share their knowledge, but at the same time, I think that the topic "advanced" is the most inflated ever, daily, and for everything about JavaScript language.

I hope you will agree with me.

Kind Regards,
Andrea Giammarchi

P.S. for the slice, and to avoid compatibility problems, use the zero value as second argument

Hi Andrea and thank you for your input, especially as you are raising great points.

The first one would be about the "advanced" term I've used in the article's title. You're right in the fact that this might not be as advanced as you'd wish but where would you draw the line ? I personally think that nowadays, when you write some code that go away from the rails of mootools, JQuery, Prototype or any other super easy to use Javascript frameworks, I'm tempted to call it advanced Javascript. Language features such as "prototype", "callee" or "arguments" might not be on everyone's Javascript vocabulary either.

While you are perfectly right with the "IsNotEmpty" and default arguments techniques, these might respectively be useful for simple cases and for code clarity.

Finally, all these samples were taken from the JsLibs wiki which you could enhance if you'd like. I'm sure lots of people will benefit from your input.