Buon di a tutto il forum..
all'epoca in cui Andrea Giammarchi parlava con me di JSL e la stava sviluppando
(e io testavo) ho avuto una lieve diatriba con lui,
perche' nella JSL voleva inserire object.prototype.toSource,
come ci si puo' aspettare, questo comporta la "rottura" del for in usato con l'oggetto
ad esempio:
codice:
<html>
<head>
<script src="jsl.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
onload=function(){
var log=document.getElementById("log");
log.write = function(str){
this.innerHTML = this.innerHTML + "
"+ str;
}
var objlit= {};
var obj = new Object();
objlit.a=12;
obj.a=12;
for(var x in objlit){
log.write("object literal prop["+x+"] = "+objlit[x]);
}
for(var x in obj){
log.write("object prop["+x+"] = "+obj[x]);
}
}
</script>
</head>
<body>
<div id="log">
</div>
</body>
</html>
che riporta erroneamente toString.
ora Dean Edwards ha recentemente publicato una estensione della funzione Array.forEach
(che potete trovare qui
che io ho leggermente modificato in maniera da togliere istanceof.
codice:
<html>
<head>
<script src="jsl.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
/*FIX TYPE OF FUNCTION-REGEX http://www.uselesspickles.com/blog/2...entity-crisis/ */
//Questa funzione serve per scoprire se una funzione e' una funzione o una regExp
function isFunction(x){
return(typeof(x)==="function" && (typeof(x.toSource)==="undefined" || x.toSource().charAt(0)!="/"))
}
if (!Array.forEach) { // mozilla already supports this
Array.forEach = function(object, block, context) {
var max=object.length;
for (var i = 0; i < max; i++) {
block.call(context, object[i], i, object);
}
};
}
Function.prototype.forEach = function(object, block, context) {
for (var key in object) {
if (typeof(this.prototype[key]) === "undefined") {
block.call(context, object[key], key, object);
}
}
};
// globally resolve forEach enumeration
var forEach = function(object, block, context) {
if (object) {
var resolve = Object; // default
//if (object instanceof Function) {
if(isFunction(object)){
// functions have a "length" property
resolve = Function;
} else if (isFunction(object.forEach)) {
// the object implements a custom forEach method so use that
object.forEach(block, context);
return;
} else if (typeof(object.length) == "number") {
// the object is array-like
resolve = Array;
}
resolve.forEach(object, block, context);
}
};
onload=function(){
var log=document.getElementById("log");
log.write = function(str){
this.innerHTML = this.innerHTML + "
"+ str;
}
var objlit= {};
var obj = new Object();
objlit.a=12;
obj.a=12;
forEach(objlit,function(x,y){
log.write("forEach object literal prop["+y+"] = "+x);
}
);
forEach(obj,function(x,y){
log.write("forEach object prop["+y+"] = "+x);
}
);
}
</script>
</head>
<body>
<div id="log">
</div>
</body>
</html>
compatibilita? IE 4+
non so ma penso anche ie5 mac vada.
Come si puo' vedere utilizzando forEach non ci sono problemi di sorta, allora perche' non estendere Object.prototype?
io non sono ancora convinto che sia una buona pratica, soprattutto perche' molti script assumono che la var in funzioni correttamente su un Object,
ma resta il fatto che usando JSL e il fix sopra riportato si puo' programmare tranquillamente senza problemi di sorta.
per chi volesse testare ecco la JSL.