ADD betterjs qui regroupe les améliorations ajoutés à javascript (each, etc..)
[euphorik.git] / js / betterjs.js
1 // tout un tas d'améliorations de JavaScript ;)
2
3
4 /**
5 * Pour chaque propriété de l'objet execute f(p, v) ou p est le nom de la propriété et v sa valeur.
6 * Ne parcours pas les propriétés des prototypes.
7 * FIXME : Normalement : Object.prototype.each = function(f) mais non supporté par jquery
8 */
9 //Object.prototype.each = function(f) {
10 var objectEach = function(o, f) {
11 for (var k in o) {
12 if (o.hasOwnProperty(k)) {
13 f(k, o[k]);
14 }
15 }
16 };
17
18 var objectMemberCount = function(o) {
19 var nb = 0;
20 for (var k in o) {
21 if (o.hasOwnProperty(k)) {
22 nb += 1;
23 }
24 }
25 return nb;
26 };
27
28 Array.prototype.each = function(f) {
29 for (var i = 0; i < this.length; i++) {
30 f(i, this[i]);
31 }
32 };
33
34 Array.prototype.map = function(f) {
35 for (var i = 0; i < this.length; i++) {
36 this[i] = f(this[i])
37 }
38 };
39
40 String.prototype.trim = function() {
41 return jQuery.trim(this) // anciennement : this.replace(/^\s+|\s+$/g, "");
42 }
43
44 String.prototype.ltrim = function() {
45 return this.replace(/^\s+/, "");
46 }
47
48 String.prototype.rtrim = function() {
49 return this.replace(/\s+$/, "");
50 }
51
52
53 /**
54 * Voir : http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html
55 *
56 * Exemple :
57 *
58 * function Mammal(name) {
59 * this.name = name;
60 * }
61 *
62 * Cat.Inherits(Mammal);
63 * function Cat(name) {
64 * this.Super(Mammal, name);
65 * }
66 */
67 /*Object.prototype.Super = function(parent) {
68 if(arguments.length > 1) {
69 parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
70 } else {
71 parent.call( this );
72 }
73 }
74 Function.prototype.Inherits = function(parent) {
75 this.prototype = new parent();
76 this.prototype.constructor = this;
77 }*/