[英]Node's console is not showing all the object properties ... is this expected behavior?
I did a我做了一个
console.log(myURL);
and did not see the extension property并且没有看到扩展属性
console.log(myURL.extension);
but if I log it on it's own it correctly shows the value.但是如果我自己登录它,它会正确显示该值。
found is a URL object created as such: found 是一个这样创建的 URL 对象:
const url = require('url');
let myURL = new URL(test);
the missing property was added as such:添加了缺失的属性:
myURL.extension = test.split('.').pop();
The output looks like this:输出如下所示:
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: ''
}
Example code:示例代码:
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);
The reason for this behavior is because the prototype
of URL
defines autil.inspect.custom
override.这种行为的原因是因为
URL
的prototype
定义了一个util.inspect.custom
覆盖。 In Node.js v12.11.0 for example, it's defined like this:例如在 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);
}
You could override this behavior and add the extension
property as a getter to the URL
class's prototype
if you really cared about the output format:如果您真的关心输出格式,您可以覆盖此行为并将
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);
};
and then your output format will look like this:然后您的输出格式将如下所示:
> 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'
}
However, if you don't care that much then you can just accept that the output format you see is the expected behavior, and you can access the extension
property just as you normally would on any other object.但是,如果您不太在意,那么您可以接受您看到的输出格式是预期的行为,并且您可以像通常在任何其他对象上一样访问
extension
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.