Saturday, February 9, 2008

The fastest way to know if someone "did it" ...

I know I choosed a weird title, isn't it :?

Today I have two small and useful tips and tricks for JavaScript.

The first one, lets you know if someone extended the Object.prototype

if((function(k){for(k in {})return true;return false})()){
// do stuff that care about Object prototype
} else {
// do stuff that doesn't care about Object prototypes
}

Simple, quick, and dirty ;) and this is another example:

function didIt(k){for(k in {})return true;return false};

if(didIt()){
// stuff prototype
}

One thing to take care about is the usage of a function instead of a variable.
Since some script could run other scripts dinamically, you can't trust in a single time check.

The second trick is really useful for function that accept a switcher argument.
For switcher argument I mean methods or function that accept true or false as argument.
The simplest way to have a switcher is obviously sending the value, but in this case we can't have a default behaviour.

Simple is to choose a "false" behaviour as default one

function addOrRemoveStuff(dontDoIt){
if(dontDoIt){
// someone sent a true value
} else {
// default behaviour, dontDoIt is false or undefined
}
}


So what's new? Nothing, yet, but try to guess about a different default ... a true value as default ... think ... think ... think ... did you find them? :)

function addOrRemoveStuff(doIt){
doIt = !arguments.length || !!doIt;
if(doIt){
// default behaviour, doIt is true
} else {
// someone sent a false value
}
};

addOrRemoveStuff(); // true
addOrRemoveStuff(false); // false
addOrRemoveStuff(1); // true


This simple thing is present, for example, in jQuery.smile method.

$("div.post, p.comments").smile(); // add smiles
$("div.post, p.comments").smile(false); // remove smiles

Enjoy these tricks, and this week end as well :)

Thursday, February 7, 2008

JSmile for jQuery ;)

As I said one post ago, I've created a jQuery dedicated version of my simple JSmile project :geek:

Here you can find the demo page, qhle here the automatic generated source

What's new?
It's so simple

$(function(){
$(document.body).smile();
});


Use $("one or more smiles containers").smile(true or false) to add/remove smiles from your pages.

See you ;)

jQuery minor improvements

Update
This post has an "official" link, now, directly in devpro :)
--------------

These are just few fixes / improvements of some jQuery stuff.


(function(o){for(var k in o)jQuery[k] = o[k]})({

// IMPROVED: Evalulates a script in a global context using a specified JS version, if any
globalEval: function( data, version ) {
/** Examples
$.globalEval("let(a = 1)alert(a);"); // error
$.globalEval("let(a = 1)alert(a);", 1.7); // OK, alert 1

(function(){
var test = 1;
$.globalEval("test = 2");
alert(test); // 1
})();
alert(test); // 2
*/
data = jQuery.trim( data );
if ( data ) {
var script = document.createElement("script");
script.type = "text/javascript";
if ( version )
script.type += ";version=" + version;
if ( jQuery.browser.msie )
script.text = data;
else
script.appendChild( document.createTextNode( data ) );
with(document.getElementsByTagName("head")[0] || document.documentElement)
removeChild( appendChild( script ) );
}
},

// IMPROVED: optional strict comparation
inArray: function( elem, array, deep ) {
for ( var i = 0, length = array.length; i < length; i++ )
if ( (deep && array[ i ] === elem) || (!deep && array[ i ] == elem) )
return i;

return -1;
},

// FIXED: ( typeof array != "array" ) .... excuse me ???
makeArray: function( array ) {
if(array instanceof Array)
var ret = array.slice();
else
for(var ret = [], i = 0, length = array.length; i < length; i++)
ret[i] = array[i];
return ret;
}

});


Next post will be about my define function for jQuery, a typeOf suggestion, and finally JSmile integrated in jQuery ... so please, stay tuned :)