lave icon indicating copy to clipboard operation
lave copied to clipboard

Constructor serialization

Open wizzard0 opened this issue 9 years ago • 0 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)

wizzard0 avatar Nov 01 '16 13:11 wizzard0