Turning JSON strings into objects with methods

前端 未结 8 487
抹茶落季
抹茶落季 2020-12-08 14:30

I have an app that allows users to generate objects, and store them (in a MySQL table, as strings) for later use. The object could be :

function Obj() {
             


        
8条回答
  •  情歌与酒
    2020-12-08 15:31

    JavaScript is prototype based programming language which is classless language where object orientation achieved by process of cloning existing objects that serve as prototypes.

    Serializing JSON would be considering any methods, for instance if you have an object

    var x = {
        a: 4
        getText: function() {
           return x.a;
        }
    };
    

    You will get just { a:4 } where getText method is skipped by the serializer.

    I ran into this same trouble a year back and I had to maintain a separate helper class for each of my domain object and used $.extend() it to my deserialized object when need, just more like having methods to a base class for the domain objects.

提交回复
热议问题