javascript - How to JSON.stringify and JSON.parse without getting an empty object? - Stack Overflow

the reason I am asking this question is because I want to use LocalStorage for my objects. And as you m

the reason I am asking this question is because I want to use LocalStorage for my objects. And as you might know, when using LocalStorage you have to JSON.stringify the objects and then parse them back to javascript objects.

I am trying to JSON.stringify an object with methods and then parse it back but all I get is an empty object.

Please take a look at this code.

Person.js

function Person(name, telePhone) {

this.getName = function () {
    return name;
}

this.setName = function (_name) {
    name = _name;
}

this.getTelePhone = function () {
    return telePhone;
}

this.setTelePhone = function (_telePhone) {
    telepPhone = _telePhone;
}
};

Javascript.js

window.onload = function () {

var name = "John";
var telePhone = "073-2335662";

var personObject = new Person(name, telePhone);

console.log(personObject);
// returns: Person 
console.log(personObject.getName());
//returns: John

var test = JSON.stringify(personObject);
var newPersonObject = JSON.parse(test);

console.log(newPersonObject);
//returns: Object
console.log(newPersonObject.getName());
//returns: Uncaught TypeError: Object #<Object> has no method 'getName'
};

Any suggestions why this Person object after JSON.stringify and JSON.parse is empty and loses all it's methods?

the reason I am asking this question is because I want to use LocalStorage for my objects. And as you might know, when using LocalStorage you have to JSON.stringify the objects and then parse them back to javascript objects.

I am trying to JSON.stringify an object with methods and then parse it back but all I get is an empty object.

Please take a look at this code.

Person.js

function Person(name, telePhone) {

this.getName = function () {
    return name;
}

this.setName = function (_name) {
    name = _name;
}

this.getTelePhone = function () {
    return telePhone;
}

this.setTelePhone = function (_telePhone) {
    telepPhone = _telePhone;
}
};

Javascript.js

window.onload = function () {

var name = "John";
var telePhone = "073-2335662";

var personObject = new Person(name, telePhone);

console.log(personObject);
// returns: Person 
console.log(personObject.getName());
//returns: John

var test = JSON.stringify(personObject);
var newPersonObject = JSON.parse(test);

console.log(newPersonObject);
//returns: Object
console.log(newPersonObject.getName());
//returns: Uncaught TypeError: Object #<Object> has no method 'getName'
};

Any suggestions why this Person object after JSON.stringify and JSON.parse is empty and loses all it's methods?

Share Improve this question edited May 11, 2013 at 11:51 Axel asked May 11, 2013 at 2:01 AxelAxel 681 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Functions are not a valid JSON data type. And certainly the variable scope held by the functions can't be serialized.

You need to store data directly on the object to serialize it.

JSON has "object" (key/value pair) and "array" (ordered list) style structures, as well as string, number, true, false, and null.

http://json/

JSON.stringify would not serialize functions, because they are not data. JSON only stores real data and it's structure - variables, specifically. If you want to store functions as well, you can use .toString() method to convert a function source code to string, and to convert it back to function you can use eval() to create the method back, but this is a separate issue.

You will probably have to write your own serializer to perform this kind of task, the idea is pretty interesting, I will not be surprised if there is a ready solution for this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745075100a4609785.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信