Object.prototype.toJSON = function(){   
    var json = [];  
    for(var i in this){ 
         alert(this[i]);//这里正常
         this.hasOwnProperty(i);//为什么这里就报  对象不支持此属性和方法  的错误
        //if(!this.hasOwnProperty(i)) continue;    
            json.push(   
            i + " : " +   
            ((this[i] != null) ? this[i] : "")   
            );   
    }   
    return "{\n " + json.join(",\n ") + "\n}";   
}

解决方案 »

  1.   

    这段代码我在ie和chrome上 随便new了个数组跑了下 一切正常,没有出现LZ的问题
      

  2.   

    this.hasOwnProperty(i;//为什么这里就报  对象不支持此属性和方法  的错误
    你这个右括号是全角的!!
      

  3.   

    Object.prototype.toJSON = function(){   
    var json = [];  
    for(var i in this){ 
    if(!this.hasOwnProperty(i)) continue;    
    json.push(   
    i + " : " +   
    ((this[i] != null) ? this[i] : "")   
    );   
    }   
    return "{\n " + json.join(",\n ") + "\n}";   
    }
    var i = {a:'b',b:'c'};
    alert(i.toJSON());