X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Fbetterjs.js;fp=js%2Fbetterjs.js;h=d7ceb8e2b8c35c7bf83f2267e7069ff5c21404ab;hp=0000000000000000000000000000000000000000;hb=4dfbbcf0f54cb3eb70856ea404d9ecef6aec5e26;hpb=d1e6f8e7c86f1ee75e854266463541ffedb916c9 diff --git a/js/betterjs.js b/js/betterjs.js new file mode 100644 index 0000000..d7ceb8e --- /dev/null +++ b/js/betterjs.js @@ -0,0 +1,77 @@ +// tout un tas d'améliorations de JavaScript ;) + + +/** + * Pour chaque propriété de l'objet execute f(p, v) ou p est le nom de la propriété et v sa valeur. + * Ne parcours pas les propriétés des prototypes. + * FIXME : Normalement : Object.prototype.each = function(f) mais non supporté par jquery + */ +//Object.prototype.each = function(f) { +var objectEach = function(o, f) { + for (var k in o) { + if (o.hasOwnProperty(k)) { + f(k, o[k]); + } + } +}; + +var objectMemberCount = function(o) { + var nb = 0; + for (var k in o) { + if (o.hasOwnProperty(k)) { + nb += 1; + } + } + return nb; +}; + +Array.prototype.each = function(f) { + for (var i = 0; i < this.length; i++) { + f(i, this[i]); + } +}; + +Array.prototype.map = function(f) { + for (var i = 0; i < this.length; i++) { + this[i] = f(this[i]) + } +}; + +String.prototype.trim = function() { + return jQuery.trim(this) // anciennement : this.replace(/^\s+|\s+$/g, ""); +} + +String.prototype.ltrim = function() { + return this.replace(/^\s+/, ""); +} + +String.prototype.rtrim = function() { + return this.replace(/\s+$/, ""); +} + + +/** + * Voir : http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html + * + * Exemple : + * + * function Mammal(name) { + * this.name = name; + * } + * + * Cat.Inherits(Mammal); + * function Cat(name) { + * this.Super(Mammal, name); + * } + */ +/*Object.prototype.Super = function(parent) { + if(arguments.length > 1) { + parent.apply( this, Array.prototype.slice.call( arguments, 1 ) ); + } else { + parent.call( this ); + } +} +Function.prototype.Inherits = function(parent) { + this.prototype = new parent(); + this.prototype.constructor = this; +}*/