简体   繁体   English

console.log() 对象不会记录通过节点 js 控制台中的原型添加的方法。 或者如何打印原型?

[英]console.log() an object does not log the method added via prototype in node js console. Or how to print the prototypes also?

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.最后一个 console.log() 不会通过点原型打印新添加的名为“getName”的方法,只打印属性“name”,在这里我希望在 Person 中打印属性“name”和方法“getName”目的。 Below is the actual output and desired output for the above code:以下是上述代码的实际输出和所需输出:

Actual output实际产量

Tinu蒂努
Person { name: 'Tinu' }人{名称:'Tinu'}

Desired output期望输出

Tinu蒂努
Person { name: 'Tinu', getName: [Function] }人{名称:'Tinu',getName:[功能]}

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.下图显示了另一个示例,其中通过原型添加的方法“getFullName”在打印以控制台添加它的对象时正确显示。 And was expecting the same with my example并期望与我的示例相同

图像在这里

In chrome dev tools, if you click the unfold icon you can see the prototype properties in __proto__ :在 Chrome 开发工具中,如果您单击展开图标,您可以在__proto__看到原型属性:

截屏

You can see that getName() is defined there.您可以看到getName()在那里定义。 That's the proper place for it since it's a property of the prototype, not the person object itself.这是它的正确位置,因为它是原型的属性,而不是 person 对象本身。

console.log is a provided API by your js-environment (in your case Node.js). console.log是您的 js 环境(在您的情况下为 Node.js)提供的 API。 There's no standard spec.没有标准规格。 So in your case console.log prints a simple string representation of your Javascript-object.因此,在您的情况下, console.log打印您的 Javascript 对象的简单字符串表示形式。

{ propName: propValue }

In Node.js there's a util-module ( util-documentation ).Node.js 中有一个 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.F12 ,你可以做很多调查。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM