Nodejs Object to String Convert toSource(object)

Here you can find the source of toSource(object)

Method Source Code

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;
   }/*  w w w . j a  v  a  2  s . c  o  m*/
   return String(object);
};

Related

  1. toSource(seen)
    Object.prototype.toSource = function(seen){
      var source, props = Object.getOwnPropertyNames(this), i = 0, j = props.length, prop;
      seen = seen || [this];
      source = '({';
      for(;i<j;i++){
        prop = props[i];
        source+= prop.toSource() + ': ' + Object.sourceOf(this[prop], seen, '{}');
        if( i < j - 1 ) source += ', ';
      source+= '})';
      return source;
    };
    
  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();
    };