这段代码有什么问题:
function obj2string(obj) {
var result = '';
for(var i in obj) {
if(typeof(obj[i]) === 'object') {
result += obj2string(obj[i]);
} else {
result += i + " => " + obj[i] + "\n";
}
}
return result;
}
它应该递归地用新属性集中结果字符串,但是在某些时候递归太多了。
我正在传递这样一个对象:$(this);
-> 来自 jQuery。
$(this)
作为这个 jQuery 选择器的实例:$('.debug');
女巫在当前文档中有一个匹配的类。
最佳答案
if(typeof(obj[i]) === 'object') {
将在 obj[i]
为 null
时执行.你知道吗?尝试使用 $.isPlainObject()
( source )
关于javascript - "too much recursion"在这个用 JavaScript 编写的递归函数中 (obj2str),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7012142/