defineToJSON.mjs 563 B

123456789101112131415
  1. import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
  2. /**
  3. * The `defineToJSON()` function defines toJSON() and inspect() prototype
  4. * methods, if no function provided they become aliases for toString().
  5. */
  6. export default function defineToJSON(classObject) {
  7. var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : classObject.prototype.toString;
  8. classObject.prototype.toJSON = fn;
  9. classObject.prototype.inspect = fn;
  10. if (nodejsCustomInspectSymbol) {
  11. classObject.prototype[nodejsCustomInspectSymbol] = fn;
  12. }
  13. }