Turning JSON strings into objects with methods

前端 未结 8 430
抹茶落季
抹茶落季 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:12

    Try to use toString on the method.

    Update:

    Iterate over the methods in obj and store them as string, and then instantiate them with new Function.

    storedFunc = Obj.prototype.setLabel.toString();
    Obj2.prototype['setLabel'] = new Function("return (" + storedFunc + ")")();
    

提交回复
热议问题