MakeClass() es un constructor de clases genérico que permite generar correctamente un nuevo objeto sólo cuando va a utilizarse correctamente ( en new User(), pero no en User() ).
// makeClass - By John Resig (MIT Licensed)
function makeClass(){
return function(args){
if ( this instanceof arguments.callee ) {
if ( typeof this.init == "function" )
this.init.apply( this, args.callee ? args : arguments );
} else
return new arguments.callee( arguments );
};
}
En Javascript Method Overloading se consigue mapear una sola llamada a una función a distintas funciones según los parámetros que acepte.
// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
var old = object[ name ];
object[ name ] = function(){
if ( fn.length == arguments.length )
return fn.apply( this, arguments );
else if ( typeof old == 'function' )
return old.apply( this, arguments );
};
}
Por último, Array Remove permite eliminar uno o un grupo de elementos de un Array de forma rápida y efectiva.
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
Como dice John: esto es lo que me gusta de JavaScript, puedes escribir una función de 3 líneas para una operación trivial, pero aún necesitas 800 palabras para explicar su verdadera naturaleza.
No hay comentarios:
Publicar un comentario