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
| <!DOCTYPE html>
<html lang="fr">
<head><meta charset="utf-8"></head>
<body>
Testez l'un ou l'autre, pas les deux en même temps.<br><br>
<div onclick="creer(tutu);" style="cursor:pointer;">Créer sur le prototype "tutu" (code plus complexe, normalement plus lent à l'utilisation des méthodes, mais 409 Mo en mémoire sur Firefox)</div><br>
<div onclick="creer(titi);" style="cursor:pointer;">Créer sur le prototype "titi" (code plus simple, normalement plus rapide à l'utilisation des méthodes, mais 1097 Mo en mémoire sur Firefox)</div><br>
Puis capturez un instantané (F12 puis onglet "Mémoire").
<script>
var tutu=(function()
{
var _verrou;
_verrou=true;
return {
initialiser:function()
{
var ob_prive={nb_1:0,nb_2:0,nb_3:0,nb_4:0,nb_5:0,ar_1:[]};
this._getVariablePrivee=function(st_nom) {if(_verrou!==false) {throw "erreur";} return ob_prive[st_nom];};
this._setVariablePrivee=function(st_nom,mx_valeur) {if(_verrou!==false) {throw "erreur";} ob_prive[st_nom]=mx_valeur;}
//alternative, mais 100 Mo de plus
/*var nb_1=0,nb_2=0,nb_3=0,nb_4=0,nb_5=0,ar_1=[];
this._getVariablePrivee=function(st_nom) {if(_verrou!==false) {throw "erreur";} return eval(st_nom);};
this._setVariablePrivee=function(st_nom,mx_valeur) {if(_verrou!==false) {throw "erreur";} eval(st_nom+"=mx_valeur");}*/
},
getNombre1:function()
{
var nb_;
_verrou=false;nb_=this._getVariablePrivee("nb_1");_verrou=true;
return nb_;
},
setNombre1:function(nb_)
{
if(typeof nb_!=="number")
{
throw "erreur";
}
_verrou=false;this._setVariablePrivee("nb_1",nb_);_verrou=true;
},
getNombre2:function(){var nb_;_verrou=false;nb_=this._getVariablePrivee("nb_2");_verrou=true;return nb_;},
setNombre2:function(nb_){if(typeof nb_!=="number"){throw "erreur";}_verrou=false;this._setVariablePrivee("nb_2",nb_);_verrou=true;},
getNombre3:function(){var nb_;_verrou=false;nb_=this._getVariablePrivee("nb_3");_verrou=true;return nb_;},
setNombre3:function(nb_){if(typeof nb_!=="number"){throw "erreur";}_verrou=false;this._setVariablePrivee("nb_3",nb_);_verrou=true;},
getNombre4:function(){var nb_;_verrou=false;nb_=this._getVariablePrivee("nb_4");_verrou=true;return nb_;},
setNombre4:function(nb_){if(typeof nb_!=="number"){throw "erreur";}_verrou=false;this._setVariablePrivee("nb_4",nb_);_verrou=true;},
getNombre5:function(){var nb_;_verrou=false;nb_=this._getVariablePrivee("nb_5");_verrou=true;return nb_;},
setNombre5:function(nb_){if(typeof nb_!=="number"){throw "erreur";}_verrou=false;this._setVariablePrivee("nb_5",nb_);_verrou=true;},
getTableau1:function(){var ar_;_verrou=false;ar_=this._getVariablePrivee("ar_1").slice(0);_verrou=true;return ar_;},
setTableau1:function(ar_){if((ar_ instanceof Array)===false || ar_.length>10){throw "erreur";}_verrou=false;this._setVariablePrivee("ar_1",ar_);_verrou=true;}
};
}());
var titi=(function()
{
return {
initialiser:function()
{
var nb_1=0,nb_2=0,nb_3=0,nb_4=0,nb_5=0,ar_1=[];
this.getNombre1=function() {return nb_1;};
this.setNombre1=function(nb_) {if(typeof nb_!=="number") {throw "erreur";} nb_1=nb_;};
this.getNombre2=function() {return nb_2;};
this.setNombre2=function(nb_) {if(typeof nb_!=="number") {throw "erreur";} nb_2=nb_;};
this.getNombre3=function() {return nb_3;};
this.setNombre3=function(nb_) {if(typeof nb_!=="number") {throw "erreur";} nb_3=nb_;};
this.getNombre4=function() {return nb_4;};
this.setNombre4=function(nb_) {if(typeof nb_!=="number") {throw "erreur";} nb_4=nb_;};
this.getNombre5=function() {return nb_5;};
this.setNombre5=function(nb_) {if(typeof nb_!=="number") {throw "erreur";} nb_5=nb_;};
this.getTableau1=function() {return ar_1.slice(0);};
this.setTableau1=function(ar_) {if((ar_ instanceof Array)===false || ar_.length>10) {throw "erreur";} ar_1=ar_;}
},
};
}());
function creer(ob_modele)
{
var ar_2,i,ob_1,ob_2;
ar_=[];
i=1000000;
while(--i>=0)
{
(ar_[i]=Object.create(ob_modele)).initialiser();
}
ob_1=ar_[0];
ob_2=ar_[1];
ob_1.setNombre1(78);
ob_2.setNombre1(13);
ob_1.setTableau1([1,2,3]);
ar_2=ob_1.getTableau1();
ar_2[0]=4;
console.log(ob_1.getNombre1(),ob_2.getNombre1(),ar_2,ob_1.getTableau1()); //78 13 [4,2,3] [1,2,3]
ob_1._setVariablePrivee(); //erreur
}
var ar_;
</script>
</body>
</html> |
Partager