javascript - Node 的控制台没有显示所有对象属性...这是预期的行为吗?

标签 javascript node.js

我做了

  console.log(myURL);

并且没有看到扩展属性

  console.log(myURL.extension);

但是如果我自己登录它,它会正确显示该值。

found 是一个 URL 对象,如下创建:

  const url = require('url');
  let myURL = new URL(test);

缺失的属性是这样添加的:

  myURL.extension = test.split('.').pop();

输出如下所示:

URL {
  href: 'https://www.imdb.com/favicon.ico',
  origin: 'https://www.imdb.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'www.imdb.com',
  hostname: 'www.imdb.com',
  port: '',
  pathname: '/favicon.ico',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

示例代码:

const url = require('url');
const test = 'https://www.imdb.com/favicon.ico';
let myURL = new URL(test);
myURL.extension = test.split('.').pop();
console.log(myURL);

最佳答案

出现此行为的原因是 prototype URL 定义了 util.inspect.custom 覆盖。例如,在 Node.js v12.11.0 中,它的定义如下:

> console.log(myURL[util.inspect.custom])

[inspect.custom](depth, opts) {
  if (this == null ||
      Object.getPrototypeOf(this[context]) !== URLContext.prototype) {
    throw new ERR_INVALID_THIS('URL');
  }

  if (typeof depth === 'number' && depth < 0)
    return this;

  const ctor = getConstructorOf(this);

  const obj = Object.create({
    constructor: ctor === null ? URL : ctor
  });

  obj.href = this.href;
  obj.origin = this.origin;
  obj.protocol = this.protocol;
  obj.username = this.username;
  obj.password = this.password;
  obj.host = this.host;
  obj.hostname = this.hostname;
  obj.port = this.port;
  obj.pathname = this.pathname;
  obj.search = this.search;
  obj.searchParams = this.searchParams;
  obj.hash = this.hash;

  if (opts.showHidden) {
    obj.cannotBeBase = this[cannotBeBase];
    obj.special = this[special];
    obj[context] = this[context];
  }

  return inspect(obj, opts);
}

您可以覆盖此行为并添加 extension属性作为URL的 setter/getter 类(class)prototype如果您真的关心输出格式:

const { URL } = require('url');
const { inspect } = require('util');

Object.defineProperty(URL.prototype, 'extension', {
  enumerable: true,
  configurable: true,
  get() { return this.pathname.split('.').pop(); }
});

URL.prototype[inspect.custom] = function(depth, opts) {
  if (typeof depth === 'number' && depth < 0) return this;

  const keys = Object.keys(URL.prototype).filter(key => typeof this[key] !== 'function');
  const obj = Object.create({ constructor: URL });
  Object.assign(obj, ...keys.map(key => ({ [key]: this[key] })));
  return inspect(obj, opts);
};

然后你的输出格式将如下所示:

> new URL('https://www.imdb.com/favicon.ico')
URL {
  href: 'https://www.imdb.com/favicon.ico',
  origin: 'https://www.imdb.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'www.imdb.com',
  hostname: 'www.imdb.com',
  port: '',
  pathname: '/favicon.ico',
  search: '',
  searchParams: URLSearchParams {},
  hash: '',
  extension: 'ico'
}

但是,如果您不太关心,那么您可以接受您看到的输出格式是预期的行为,并且您可以访问 extension属性就像您通常对任何其他对象所做的那样。

关于javascript - Node 的控制台没有显示所有对象属性...这是预期的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347988/

相关文章:

node.js - 如何自动化 Google Drive Docs OCR 功能?

javascript - 对同一个数组运行两个 for 循环

javascript - PHP 变量值未显示在 JS 警报中

javascript - JS AJAX typeahead 结果排序/竞争条件

mysql - Sequelize 多连接中的排序错误

node.js - Docker 公开端口 - OCI 运行时创建失败

javascript - 如果 (Var == True) { 这样做! }

javascript - 在方形空间 (YUI) 上重绘元素的问题

javascript - 使用 Heroku 部署 Node.js 应用程序时出现公共(public)目录问题

node.js - SASS 的 Node 和 Gulp - 堆栈级别太深