js中JSON.stringify用于自定义的类

参考:http://stackoverflow.com/questions/7356694/how-to-json-stringify-a-user-defined-class-in-javascript

 

function MyClass() {
    this.a = "1a";
    this.b = "1b";
    this.c = 100;
    this.d = {
        da : "1da",
        dc : 200
    };
}

MyClass.prototype.isManual = function() {
    return true;
}

var myClass = new MyClass();
var json = JSON.stringify(myClass);
console.log(json)

console.log(myClass.isManual())
var json = JSON.stringify({"myClass": myClass, "haha": 1});
console.log(json)

结果为:

{"a":"1a","b":"1b","c":100,"d":{"da":"1da","dc":200}}
true
{"myClass":{"a":"1a","b":"1b","c":100,"d":{"da":"1da","dc":200}},"haha":1}

 

转载于:https://www.cnblogs.com/bonelee/p/6139013.html

你可能感兴趣的:(js中JSON.stringify用于自定义的类)