简体   繁体   English

Error 对象似乎如何在 Javascript 上自行创建属性?

[英]How does the Error object seems to create properties on itself by his own on Javascript?

Ok I'd first like to say that I know there's probably a very basic explanation for this, so excuse me if this is a way too newbie kind of question.好的,我首先想说我知道对此可能有一个非常基本的解释,所以如果这是一个过于新手的问题,请原谅我。

The question is: How come the Error.message property doesn't exist until I create a new prototype?问题是:为什么在我创建新原型之前 Error.message 属性不存在?

Example:例子:

let a = new Error('testing')
console.log( a.message ) // 'testing'

But before if I tried to access the Error properties and methods (eg: Object.getOwnPropertyNames(Error) ) the message property wouldn't even be there, not even an empty string, it just wouldn't exist.但在之前,如果我尝试访问 Error 属性和方法(例如: Object.getOwnPropertyNames(Error) ),则消息属性甚至都不存在,甚至不存在空字符串,它只是不存在。

Shouldn't it have been an empty string before?之前不应该是空字符串吗? How does the Error object creates a new property on its own when a new prototype is created?创建新原型时,Error 对象如何自行创建新属性?

getOwnPropertyNames only returns properties set on an object , not inherited (prototype) properties. getOwnPropertyNames只返回对象设置的属性,而不是继承的(原型)属性。 The clue is in the name: " own "-properties.线索就在名称中:“自己的”-properties。

By example:举例:

const a_simple_object_with_no_prototype = {
    b: "b",
    c: "c"
};

console.log( Object.getOwnPropertyNames( a_simple_object_with_no_prototype ) )
// [ "b", "c" ]

With an object with properties inherited from a prototype we get different results:对于具有从原型继承的属性的对象,我们会得到不同的结果:

const an_object_used_as_a_prototype = {
    a: 789
};

function ConstructorFunction() {
    this.b = "b";
}
ConstructorFunction.prototype = an_object_used_as_a_prototype;

const an_object_with_a_prototype = new ConstructorFunction();
an_object_with_a_prototype.c = "c";


console.log( Object.getOwnPropertyNames( an_object_with_a_prototype ) )
// [ "b", "c" ] // does not include "a" which is inherited from the prototype

But before if I tried to access the Error properties and methods (eg: Object.getOwnPropertyNames(Error) ) the message property wouldn't even be there, not even an empty string, it just wouldn't exist.但在之前,如果我尝试访问 Error 属性和方法(例如: Object.getOwnPropertyNames(Error) ),则消息属性甚至都不存在,甚至不存在空字符串,它只是不存在。

That's because Object.getOwnPropertyNames(Error) will only list the "static" 1 members of the Error constructor function.那是因为Object.getOwnPropertyNames(Error)只会列出Error构造函数的“静态” 1成员。 If you do Object.getOwnPropertyNames( Error.prototype ) then you'll see them:如果你执行Object.getOwnPropertyNames( Error.prototype )那么你会看到它们:

const protoProps = Object.getOwnPropertyNames( Error.prototype );
console.log( protoProps ); // ["constructor", "name", "message", "toString"]

I do note that the Error object in JavScript is imbued with some special behaviour which you cannot replicate in JavaScript itself, such as how its stack property works, but that's outside the scope of your question.我确实注意到,JavScript 中的Error对象充满了一些您无法在 JavaScript 本身中复制的特殊行为,例如其stack属性的工作方式,但这超出了您的问题范围。

1 : I use the term "static" in the C++ / Java / C# sense in that it's a field/member that is associated with a type rather than an instance . 1 :我在 C++/Java/C# 意义上使用术语“静态”,因为它是与类型而非实例相关联的字段/成员。 It does not mean the property/field/member is unchanging, readonly, nor immutable.这并不意味着属性/字段/成员是不变的、只读的或不可变的。

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

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