Javascript中模拟PHP的print_r函数


有些效果出不来,比如变量类型是object类型的不能完全打印!

function print_r(array, return_val) {
	var output = '', pad_char = ' ', pad_val = 4, d = this.window.document,
	getFuncName = function (fn) {
		var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
		if (!name) {
			return '(Anonymous)';
		}
		return name[1];
	},
	repeat_char = function (len, pad_char) {
		var str = '';
		for (var i = 0; i < len; i++) {
			str += pad_char;
		}
		return str;
	},
	formatArray = function (obj, cur_depth, pad_val, pad_char) {
		if (cur_depth > 0) {
			cur_depth++;
		} 
		var base_pad = repeat_char(pad_val * cur_depth, pad_char);
		var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
		var str = '';
		if (typeof obj === 'object' &amp;&amp; obj !== null &amp;&amp; obj.constructor &amp;&amp; getFuncName(obj.constructor) !== 'PHPJS_Resource') {
			str += 'Array\n' + base_pad + '(\n';
			for (var key in obj) {
				if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
					str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
				} else {
					str += thick_pad + '[' + key + '] => ' + obj[key] + '\n';
				}
			}
			str += base_pad + ')\n';
		} else if (obj === null || obj === undefined) {
			str = '';
		} else {
			str = obj.toString();
		}
		return str;
	};
 
	output = formatArray(array, 0, pad_val, pad_char);
 
	if (return_val !== true) {
		if (d.body) {
			this.echo(output);
		} else {
			try {
				d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
				this.echo('<'+'pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</'+'pre>');
			} catch (e) {
				this.echo(output); // Outputting as plain text may work in some plain XML
			}
		}
		return true;
	}
	return output;
}


13年前10月8日 阅读:133 评论:0

青锋幽灵

゛. - 看過世间最冷漠滴眼神,  爱過⒈生最无缘滴人﹎

评论 More..

该页面还没有任何评论,赶快占个沙发吧!

登录 注册

您没有登录,如果还不是会员请先注册

文明上网,理性发帖!


顶部