最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - console.log() an object does not log the method added via prototype in node js console. Or how to print the prototy

programmeradmin5浏览0评论
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

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

Share Improve this question edited Dec 8, 2019 at 7:22 connexo 56.9k15 gold badges111 silver badges146 bronze badges asked Dec 7, 2019 at 17:39 Tinu Jos KTinu Jos K 9601 gold badge9 silver badges23 bronze badges 4
  • Well what you expect would be very annoying. Let alone the fact that this is not what Javascript getters look like. – connexo Commented Dec 7, 2019 at 17:55
  • Could you please elaborate? – Tinu Jos K Commented Dec 7, 2019 at 18:25
  • @Tick20 wele to SO! Did you solve question? – wuarmin Commented Dec 8, 2019 at 9:24
  • @wuarmin my question is still not pletely 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 Commented Dec 9, 2019 at 5:26
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论