不允许枚举跨原点对象-对于对象数组中的(i in x)
Not allowed to enumerate cross origin objects - for(i in x) in array of objects
我不明白。我做了一个简单的递归函数来搜索我传递给它的任何对象。
除非我尝试给它一个窗口属性的数组(或对象)作为第一个参数,否则它会像我预期的那样工作。
window.search_object([window.API, window.Chat], /vote/); //Error: Not allowed to enumerate cross origin objects
window.search_object({x:window.API, y:window.Chat}, /vote/); //Error: Not allowed to enumerate cross origin objects
错误发生在for(var i in object)
的线路上。
对象搜索功能:
window.search_object = function(object, search_string, max_recursion, search_value, history, path) {
//This will be returned in the end
var results = [];
//Just ignore document object
if(object == window.document)
return [];
//Only created in level 1 function, others get this passed by parameter
if(history==null)
history = {data:[]};
//Simplest way to find the origin of found values
if(path==null)
path = [];
//Convert sring to regext (I'm lazy)
if(typeof search_string=="string") {
search_string = new RegExp(search_string);
}
//Some errors
if(!(search_string instanceof window.RegExp)) {
console.log([search_string, search_string instanceof window.RegExp])
throw new Error("search_object() -> Parameter 2 (searched string) must be a string or RegExp object.");
}
if(typeof object != "object") {
throw new Error("search_object() -> Parameter 1 (object to search in) must be a Javascript object");
}
//Loop
for(var i in object) { //<-------THIS IS THE ERROR LINE
//Test if the property name matches
if(search_string.test(i)) {
//Add in results (no duplicate fallback here)
results.push([object[i], path.concat(i)]);
}
//Recursion
if(typeof object[i]=="object") {
//Check if we did not parse this object already
if(history.data.find(object[i])===false) {
//Remember the object
history.data.push(object[i]);
//Add results from all sub-objects
results = results.concat(window.search_object(object[i], search_string, null, null, history, path.concat(i)));
}
}
}
//console.log(results);
return results;
}
Array.prototype.find = function(needle) {
for(var i=0; i<this.length; i++) {
if(this[i]==needle)
return i;
}
return false;
}
窗口对象具有循环引用。这将导致等待堆栈溢出。
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 在Javascript中转换对象数组
- 在JavaScript中通过索引从对象数组中获取值
- Backbone虹吸以获取对象数组
- 如何在DataTables 2.1中迭代对象数组
- Javascript-根据赋值顺序,按键合并对象数组
- 将事件附加到对象/数组
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript在数组中获取对象数组中键的所有不同值
- 在对象数组中查找多个值的d3范围
- Undercore.js获取对象数组中键对象的值
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 如何通过json对象数组为嵌套对象赋值
- 如何循环通过2个对象数组并通过匹配id进行合并
- 为对象数组创建列表项
- 如何使用javascript合并两个对象数组
- JSON到对象数组,并向每个对象添加项
- JavaScript:从对象数组中获取唯一值及其计数
- 按不同项目对对象数组进行排序