简体   繁体   English

javascript Uncaught ReferenceError:未定义uneval

[英]javascript Uncaught ReferenceError: uneval is not defined

Uncaught ReferenceError: uneval is not defined

It works well with Firefox, but not work with Google Chrome. 它与Firefox兼容,但与Google Chrome兼容。

I try: 我尝试:

var getSaved = [['ABC', 'http://...'], ['DEF', 'http://...'], ['...etc...']];
var get_saved_items = eval(getSaved);
var name = [], url = [];
for (var i = 0; i < get_saved_items.length; i++) {
    name[i].push(get_saved_items[i][0]);
    url[i].push(get_saved_items[i][1]);
};
window.localStorage.setItem('saved_name', uneval(name));
window.localStorage.setItem('saved_links', uneval(url));

it returns: Uncaught ReferenceError: uneval is not defined 它返回: Uncaught ReferenceError: uneval is not defined

What's the problem? 有什么问题?

我相信uneval只是在SpiderMonkey中,所以也只是基于Mozilla的浏览器。

Using 'eval' is not recommended because of different reasons (you can google it). 不建议使用“ eval”,因为原因不同(您可以在Google上搜索)。 uneval will not work in other browsers. uneval在其他浏览器中不起作用。

For your requirement you can use a JSON parser. 根据您的要求,您可以使用JSON解析器。 Widely used one is Crockford's JSON parser. 广泛使用的是Crockford的JSON解析器。

https://github.com/douglascrockford/JSON-js/blob/master/json2.js https://github.com/douglascrockford/JSON-js/blob/master/json2.js

var numbers = '[1, 2, 3, 4, 5, 6]'; 
var names = '["abc", "def", "ghi", "jkl", "mno", "pqr"]';

// string to Javascript object 
var numbersObject = JSON.parse(numbers);
var namesObject = JSON.parse(names);

console.log(numbersObject);
console.log(namesObject);

// Javascript object to string
numbers = JSON.stringify(numbersObject);
names = JSON.stringify(namesObject);

console.log(numbers);
console.log(names);

demo : http://jsfiddle.net/xVvBp/ 演示: http : //jsfiddle.net/xVvBp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM