Apie indų programerius dirbančius už 2 baksus per valandą
Kadangi turbūt niekam nepaslaptis, kad turiu polinkį į Php, ir visos kitose programavimo kalbose(funkcinėse, objektinėse, skriptinėse) stengiuosi emuliuoti Php galimybes (C++’e nesugebu apsieti be explode();),
tai šiandien negalėjau apsiieiti be dar vienos labai šaunios Php funkcijos – tai print_r();. Jos esmė ta, kad priešingai nei kitų prog. kalbų kurėjai, kurie neturi kažko panašaus į print_r(), print_r nuosekliai išprintina n’tai masyvo, objektų sarašo visus elementus rekursiškai – užtenka paduoti seką į parametrus
.
Taigi šios idėjos vedamas, susiradau alternatyvą print_r(); bene populiariausiai skriptine kalbai – javascript.
Po to kai programa į downtime’ą keletui minučių pasiuntė visą dedikuotą servą, atsidaręs skripto autoriaus puslapį kažkodėl nenustebau – Bandula Binny Bandulay iš Indijos
. Nebūtų non-sense’as, jeigu ne faktas – kad šito Binny’io source kodas yra pirmoje pozicijoje Google
DD.
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
* The level - OPTIONAL
* Returns : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
Norint užlenkti visą sistemą užteko ją pabandyt
:
alert(dump(document.getElementById("xc_obj").childNodes));
Moralas tas – jeigu jau dedi sūrsą, kuris gūglėj bus pirmoj pozicijoj, bent jau jį patikrink.
Nes jeigu tokie pat išminčiai būtų rašę kokį TinyMCE, ar PhpExcel, tai beieškodamas klaidų ir užmiršti kam tau to skripto reikėjo, būtų galima…
(dalyje plačiau – įmečiau pilnai gerai veikiantį kodą).
Įdomumo dėlei įdedu ir gerą pilną, Php’iaus print_r(); emuliacijos Javascript’e kodą:
function print_r(obj,pre,child)
{
if(pre === undefined) pre = false;
if(child === undefined) child = 0;
var n = "\n";
var t = " ";
var ts = "";
if(pre) for(var i = 0; i <= child; i++) ts += t;
if(obj.constructor == Array || obj.constructor == Object)
{
if(pre && child == 0)
{
document.write('<pre>'+n);
document.write('Array'+n);
document.write('('+n);
}
else if(pre && child > 0)
{
document.write(ts+'('+n);
}
else
{
document.write('Array (');
}
for(var value in obj)
{
if(obj[value].constructor == Array|| obj[value].constructor == Object)
{
var newChild = child + 1;
if(pre && child == 0)
{
document.write(ts+'['+value+'] => Array'+n);
}
else if(pre && child > 0)
{
document.write(ts+t+'['+value+'] => Array'+n);
newChild++;
}
else
{
document.write(" ["+value+"] => ");
}
print_r(obj[value],pre,newChild);
}
else
{
if(pre && child == 0)
{
document.write(t+'['+value+'] => '+obj[value]+n);
}
else if(pre && child > 0)
{
document.write(ts+t+'['+value+'] => '+obj[value]+n);
}
else
{
document.write(' ['+value+'] => '+obj[value]+' ');
}
}
}
if(pre && child == 0)
{
document.write(') ');
document.write('</pre>');
}
else if(pre && child > 0)
{
document.write(ts+') '+n);
}
else
{
document.write(') ');
}
}
}
Tai tiek
console.debug(var);
@Marius
Tačiau firebug konsolė naudinga tik konkrečioms reikšmėms tikrinti, o jeigu tai yra autogeneruojamas random žemėlapis, tai debug’ą reikia rašyti į kodą, ir kviesti po kiekvieno ciklo.
O tai print_r/dump nereikia rasyt?
Na tai aš ir dedu jį į kodą, kadangi jis vidinis yra, ir padarius F5 – automatiškai po kiekvieno ciklo ir kviečia print_r’ą pagal JO METU sugeneruotus parametrus.
O console.debug leidžia patikrinti konkrečią reikšmę ar konkretų reikšmių array’ų, bet ne kintančius duomenis. T.y. kai kažkas gražiną kažką ir tas kažkas GALI būtų neteisingas. Mes nežinome kas konkrečiu atveju bus tas kažkas, todėl to atveju greičiausiai taip ir nepatikrinsime su console.debug(). Gi nesuksim array’u iki sisksilijonijų reikšmių.