Thursday, November 25, 2010

用的Javascript函数var_dump(),implode()

[cc lang="javascript"]
// 检测是否为整数
function isInteger( str ){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}

function implode (glue, pieces) {
var i = '', retVal='', tGlue='';
if (arguments.length === 1) {
pieces = glue;
glue = '';
}
if (typeof(pieces) === 'object') {
if (pieces instanceof Array) {
return pieces.join(glue);
}
else {
for (i in pieces) {
retVal += tGlue + pieces[i];
tGlue = glue;
}
return retVal;
}
}
else {
return pieces;
}
}

function print_r(x, max, sep, l) {

l = l || 0;
max = max || 10;
sep = sep || ' ';

if (l > max) {
return "[WARNING: Too much recursion]\n";
}

var
i,
r = '',
t = typeof x,
tab = '';

if (x === null) {
r += "(null)\n";
} else if (t == 'object') {

l++;

for (i = 0; i < l; i++) {
tab += sep;
}

if (x && x.length) {
t = 'array';
}

r += '(' + t + ") :\n";

for (i in x) {
try {
r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
} catch(e) {
return "[ERROR: " + e + "]\n";
}
}

} else {

if (t == 'string') {
if (x == '') {
x = '(empty)';
}
}

r += '(' + t + ') ' + x + "\n";

}

return r;

};
var_dump = print_r;
[/cc]

No comments:

Post a Comment