I need to be able to serialize an object declared with a class in ECMA6 . I could use toJSON
easily when using standard JavaScript to get and serialize the "methods" stored in the prototype.
var Foo = function(name) {
this.name = name;
}
Foo.prototype = {
doSomething: function() {
return 1;
},
toJSON: function() {
// construct a string accessing Foo.prototype
// in order to get at functions etc. stored in prototype...
}
};
But how do I do this using class declarations in ECMA6? Example:
class Foo {
constructor(name) {
this.name = name;
}
doSomething() {
return 1;
}
toJSON() {
// Foo.prototype is empty. Because it hasn't been created yet?
}
}
Even after instantiation with the new
operator, objects created with class
seem to have an empty prototype. What am I missing?