1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="style/menu.css" type="text/css" />
<link rel="stylesheet" href="style/menuh.css" type="text/css" />
<script type="text/javascript" >
function Class(config, extend){
config.initialize = config.initialize || function(){};
//le super constructeur
var klass = function(){
if(this.initialize.overdefined){
this.initialize.overdefined[arguments.length].apply(this, arguments);
}else
this.initialize.apply(this, arguments);
};
if(extend){
// on hérite classiquement la classe
var tmp = function(){};
tmp.prototype = extend.prototype;
klass.prototype = new tmp();
//pour chaque fonction du prototype on définit la méthode inherit
var overdefined;
for(var i in config){
klass.prototype[i] = config[i];
if(typeof config[i] == 'function'){
var m = extend.prototype[i];
if(overdefined = config[i].overdefined){
for(var j in overdefined)
overdefined[j].inherit = Class.getInherit(m);
}else
config[i].inherit = Class.getInherit(m);
}
}
}else klass.prototype = config;
//return le constructeur
return klass;
}
//retourne la fonction inherit en fonction de la méthode dérivé (si surdéfinit ou pas)
Class.getInherit = function(method){
return function(obj, args){
return (method.overdefined ? method.overdefined[args.length]: method).apply(obj, args || []);
}
};
//créer une méthode surdéfinit, qui renvoie la bonne méthode en fonction du nombre de paramatre
Class.overdefine = function(){
function func(obj){
var args = Array.prototype.slice.call(arguments);
args.shift();
return func.overdefined[args.length].apply(obj, args);
}
func.overdefined = {};
func.addFunction = function(fn){
var m = fn.toString().match(/function\s*\w*\((.*)\)/);
func.overdefined[m ? m[1].split(',').length : 0] = fn;
fn.overdefined = func;
};
for(var i = 0, l = arguments.length; i < l; i++)
func.addFunction(arguments[i]);
return func;
};
var Personne = Class({
//constructeur
initialize : function(sexe, age){
this.sexe = sexe;
this.age = age;
},
affiche : function(){
return 'Je suis une personne de sexe ' +
this.sexe + ', j\'ai ' +this.age+ 'ans' +
(this.nationnalite ? ' et j\'ai la nationnalité ' + this.nationnalite + '.' : '.')+
(this.religion ? ' et je suis ' + this.religion + '.' : '.');
}
});
//rajoute une méthode surdéfinit
Personne.prototype.initialize = Class.overdefine(Personne.prototype.initialize,
function(sexe, age, nationnalite){
arguments.callee.overdefined(this, sexe, age);//appel au constructeur surdéfinit à deux parametre
this.nationnalite = nationnalite;
}
);
//rajoute une méthode aux tableau de méthode surdéfinit
Personne.prototype.initialize.addFunction(function(sexe, age, nationnalite, religion){
arguments.callee.overdefined(this, sexe, age, nationnalite);//appel au constructeur à trois parametre
this.religion = religion;
}
);
alert(new Personne('masculin', 28).affiche());
alert(new Personne('masculin', 28, 'Française').affiche());
alert(new Personne('masculin', 28, 'Française', 'athée').affiche());
var Parent = Class({
//le constructeur , surdéfinit ,
initialize : Class.overdefine(
function(sexe, age, nbEnfants){
arguments.callee.inherit(this, [sexe, age]);//appel de la méthode dérivé
this.nbEnfants = nbEnfants;
},function(sexe, age, nationnalite, nbEnfants){
arguments.callee.inherit(this, [sexe, age, nationnalite]);
this.nbEnfants = nbEnfants;
}
),
affiche : function(){
return arguments.callee.inherit(this) + 'Et j\'ai ' +this.nbEnfants+ ' enfant(s)';
}
}, Personne);
alert(new Parent('masculin', 28, 0).affiche());
alert(new Parent('masculin', 28, 'Française', 0).affiche());
</script>
</head>
<body>
</body>
</html> |