0

这个我不明白。我做了一个简单的递归函数来搜索我传递给它的任何对象。
除非我尝试将窗口属性的数组(或对象)作为第一个参数,否则它会按我的预期工作。

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;
} 
4

1 回答 1

0

窗口对象具有循环引用。这将导致——等待它——堆栈溢出。

于 2013-07-09T19:50:54.563 回答