jed / lave

eval in reverse: stringifying all the stuff that JSON.stringify won't

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constructor serialization

wizzard0 opened this issue · comments

Serializing constructor like this:

  var generate = require('escodegen').generate
  var lave = require('lave')
  var fs = require('fs')

    function Greeter(message) {
        this.greeting = message;
    }
    Greeter.prototype.greet = function () {
        return "Hello, " + this.greeting;
    };
var greeter = new Greeter("world");
  var js = lave(greeter, {generate, format: 'module'})
console.log(js);

outputs

var a = {
        'constructor': function Greeter(message) {
        this.greeting = message;
    },
        'greet': function () {
        return "Hello, " + this.greeting;
    }
    }, b = Object.create(a);
function Greeter(message) {
        this.greeting = message;
    }.prototype = a;
b.greeting = 'world';
export default b;

instead of something like

var a = {
        'constructor': function Greeter(message) {
        this.greeting = message;
    },
        'greet': function () {
        return "Hello, " + this.greeting;
    }
    }, b = Object.create(a);
function Greeter(message) {
        this.greeting = message;
    };
Greeter.prototype = a;
b.greeting = 'world';
export default b;

which results in

    }.prototype = a;
     ^
SyntaxError: Unexpected token .
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)