Closed Aaron-P closed 2 years ago
Class methods are non-enumerable by default.
If you want a specific method to be enumerable you can override the enumerable
setting:
class Test2 {
constructor() {
}
toJSON() {
return {
b: 666
};
}
}
Object.defineProperty(Test2.prototype, 'toJSON', {enumerable: true});
var test2 = new Test2();
If a class is created using ES5 'prototype' syntax and a toJSON method is defined on the prototype, that method gets carried through the ko.toJS (and thus ko.toJSON) method. For example:
Returns:
If ES6+ class syntax is used with a toJSON method, it is not carried through. For example:
Throws:
The issue seems to be in the method visitPropertiesOrArrayEntries where if toJSON is defined on the prototype it is picked up by the "for (var propertyName in rootObject)" loop, but if it's defined as a method on a class definition it does not. An explicit check for rootObject["toJSON"] may resolve the direct issue, but there may still be some additional problems if the toJSON is expecting other methods to be carried through as well and they are also missed by the "for in" loop. I'm not sure if there's a better way of handle that.