3
function Person(name)  {
        this.name = name;
    }

    Person.prototype.getName = function() {
        return this.name
    }

    var tinu = new Person('Tinu');

    console.log(tinu.getName()) //Prints the name 'Tinu' - Expected, means the function is added to protoype

    console.log(tinu);

The last console.log() does not print the newly added method named 'getName' via dot prototype, prints only the property 'name', Here I would expect to print both property 'name' and also method 'getName' inside the Person object. Below is the actual output and desired output for the above code:

Actual output

Tinu
Person { name: 'Tinu' }

Desired output

Tinu
Person { name: 'Tinu', getName: [Function] }

The image below shows another example where the method 'getFullName' added via prototype is correctly shown while printing to console the object to which it is added. And was expecting the same with my example

image here

connexo
  • 53,704
  • 14
  • 91
  • 128
Tinu Jos K
  • 888
  • 1
  • 8
  • 21
  • Well what you expect would be ***very*** annoying. Let alone the fact that this is not what Javascript getters look like. – connexo Dec 07 '19 at 17:55
  • Could you please elaborate? – Tinu Jos K Dec 07 '19 at 18:25
  • @Tick20 welcome to SO! Did you solve question? – wuarmin Dec 08 '19 at 09:24
  • @wuarmin my question is still not completely solved, but I found that using console.log(myObject.__proto__) prints the prototypes added :) and that solved half of the problem. But still searching for a way to print the prototypes along with the actual object contents itself with just one console log. – Tinu Jos K Dec 09 '19 at 05:26

2 Answers2

2

In chrome dev tools, if you click the unfold icon you can see the prototype properties in __proto__:

screenshot

You can see that getName() is defined there. That's the proper place for it since it's a property of the prototype, not the person object itself.

Pavel Lint
  • 3,252
  • 1
  • 18
  • 18
2

console.log is a provided API by your js-environment (in your case Node.js). There's no standard spec. So in your case console.log prints a simple string representation of your Javascript-object.

{ propName: propValue }

In Node.js there's a util-module (util-documentation). Furthermore I found a method, which returns all properties of an object including all properties of the prototype-chain.

const util = require('util')

function Person(name)  {
    this.name = name;
}

Person.prototype.getName = function() {
    return this.name
}

var tinu = new Person('Tinu');

console.log(util.inspect(tinu, {showHidden: false, depth: null}))

function getAllPropertyNames(obj) {
  var props = [];

  do {
    Object.getOwnPropertyNames(obj).forEach(function (prop) {
      if (props.indexOf(prop) === -1 ) {
        props.push( prop );
      }
    });
  } while (obj = Object.getPrototypeOf(obj));

  return props;
}

console.log(getAllPropertyNames(tinu)); 
/*
[ 'name',
  'constructor',
  'getName',
  '__defineGetter__',
  '__defineSetter__',
  'hasOwnProperty',
  '__lookupGetter__',
  '__lookupSetter__',
  'isPrototypeOf',
  'propertyIsEnumerable',
  'toString',
  'valueOf',
  '__proto__',
  'toLocaleString' ]
 */

If you are on a Browser and want to see defined methods and other infos, you can use your browser's developer tools. Press F12 and you can do a lot of investigation.

enter image description here

wuarmin
  • 3,274
  • 3
  • 18
  • 31
  • This answer is somewhat near to my need but is still not satisfying my need for printing methods added via the prototype, According to https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object this method can be used to show nested objects, but i am still not able to view the prototypes. – Tinu Jos K Dec 09 '19 at 05:21
  • @Tick20 I updated my answer, with method, which prints all properties of an object, including props of the prototype-chain – wuarmin Dec 09 '19 at 13:19
  • Thanks, but I thought there could be some easy way to print the object and prototypes. But as of now, I believe we need to iterate over the properties of the objects with getOwnProperyNames like what you have done. – Tinu Jos K Dec 09 '19 at 13:46
  • @TinuJosK If you have got any solution that you were looking for please do post here. – utkarsh-k Jul 09 '22 at 19:18
  • @utkarsh-k Sorry buddy, I think I didn't. – Tinu Jos K Jul 18 '22 at 18:05