如何用JavaScript实现类似PHP的var_dump()功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计139个文字,预计阅读时间需要1分钟。
javascript// 模拟PHP中的var_dump函数,可以查看数组或对象的详细信息function var_dump(obj) { if (typeof obj===object) { console.log(Type: + typeof obj); console.log(Properties:); for (var key in obj) { console.log(key + : + obj[key]); } } else { console.log(Type: + typeof obj); console.log(Value: + obj); }}
不过说回来,这个是个伪需求,因为console.info就能实现啦.
1.[代码][JavaScript]代码
function var_dump(obj) { if(typeof(obj) == "object") { var txt = ''; for(key in obj) { txt += key + '=' + obj[key] + ','; } return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + txt; } else { return "Type: "+typeof(obj)+"\nValue: "+obj; } }
本文共计139个文字,预计阅读时间需要1分钟。
javascript// 模拟PHP中的var_dump函数,可以查看数组或对象的详细信息function var_dump(obj) { if (typeof obj===object) { console.log(Type: + typeof obj); console.log(Properties:); for (var key in obj) { console.log(key + : + obj[key]); } } else { console.log(Type: + typeof obj); console.log(Value: + obj); }}
不过说回来,这个是个伪需求,因为console.info就能实现啦.
1.[代码][JavaScript]代码
function var_dump(obj) { if(typeof(obj) == "object") { var txt = ''; for(key in obj) { txt += key + '=' + obj[key] + ','; } return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + txt; } else { return "Type: "+typeof(obj)+"\nValue: "+obj; } }

