Nodejs Object to String Convert toSource(seen)

Here you can find the source of toSource(seen)

Method Source Code

Object.prototype.toSource = function(seen){
   var source, props = Object.getOwnPropertyNames(this), i = 0, j = props.length, prop;

   seen = seen || [this];/*ww  w  .  jav  a  2  s.  com*/
   source = '({';
   for(;i<j;i++){
      prop = props[i];
      source+= prop.toSource() + ': ' + Object.sourceOf(this[prop], seen, '{}');
      if( i < j - 1 ) source += ', ';
   }
   source+= '})';

   return source;
};

Related

  1. toSource(object)
    Object.toSource = function(object){
      if( object === null ) return 'null';
      if( object === undefined ) return 'undefined';
      if( typeof object.toSource === 'function' ){
        var source = object.toSource();
        if( typeof source != 'string' ) throw new TypeError('custom toSource() method must return string');
        return source;
      return String(object);
    ...
    
  2. json()
    Object.prototype.json = function() { 
        return JSON.stringify(this) 
    
  3. to_s()
    Object.prototype.to_s = function () {
      try {
        return JSON.stringify(this.valueOf());
      } catch (ex) {
        return this.valueOf().toString();
    };