javascript - Node 的控制台没有显示所有对象属性……这是预期的行为吗?
问题描述
我做了一个
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
ofURL
定义了一个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
属性作为 getter 添加到URL
类中: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
像通常在任何其他对象上一样访问该属性。
推荐阅读
- animation - 4x4 矩阵 (3D) 动画
- bash - 在 bash 中将大型数据集从宽格式转换为长格式(使用 awk 或类似方法)
- python - 处理带有连词的句子的 NLP 问题
- powershell - 密码身份验证在 Windows 上的 ssh 中不起作用
- sql - SQL 查询选择所有有商店但没有商店的用户
- python - 打印分成许多列表的字符串
- amazon-web-services - 从 NLB 到达 Nat 实例
- c# - 如何在 C# 中将多维数组解释为一维数组,反之亦然
- r - 有没有办法为包含“facet_wrap()”的ggplot图设置不同的“y”限制?
- machine-learning - CatBoost 中的 CTR 是什么意思