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
| <html>
<head>
<script type="text/javascript">
var LoggerFactory = function(){
if ( LoggerFactory.caller != LoggerFactory.getInstance ) {
throw new Error("This object cannot be instanciated");
}
LoggerFactory.prototype.buildLogger = function(params){
var Logger = function(width, height, x, y){
this.loggerDiv = document.createElement("div");
this.loggerDiv.style.backgroundColor = "red";
this.loggerDiv.style.position = "absolute";
this.loggerDiv.style.top = y+"px";
this.loggerDiv.style.left = x+"px";
this.loggerDiv.style.width = width+"px";
this.loggerDiv.style.height = height+"px";
document.body.appendChild(this.loggerDiv);
}
Logger.prototype.log = function(chaine) {
this.loggerDiv.innerHTML = "<div>"+chaine+"</div>"+this.loggerDiv.innerHTML;
}
return new Logger(params.width, params.height, params.x, params.y);
}
};
/* fonction anonyme "auto-exécutée" pour le singleton */
(function(){
var instance = null;
LoggerFactory.getInstance = function() {
if (instance == null){
instance = new LoggerFactory();
}
return instance;
}
})();
var debug1 = null;
var debug2 = null;
window.onload = function(){
var loggerFactory = LoggerFactory.getInstance();
debug1 = loggerFactory.buildLogger({"width":400,"height":300,"x":50,"y":50});
debug2 = loggerFactory.buildLogger({"width":200,"height":200,"x":400,"y":300});
debug1.log("test1");
debug2.log("test2");
}
</script>
</head>
<body>
</body>
</html> |
Partager