这些神秘的javascript异常方法是什么?

时间:2022-10-17 22:42:33

I examined a javascript exception in Google Chrome.

我在Google Chrome中检查了一个javascript异常。

这些神秘的javascript异常方法是什么?

And I noticed the functions get message, get stack, set message, and set stack. I tried catching this exception and running alert(e.get_message()); only to get an error. I also tried to run alert(e.get message());, which obviously returned another error due to the space.

我注意到函数获取消息,获取堆栈,设置消息和设置堆栈。我尝试捕获此异常并运行alert(e.get_message());只是为了得到一个错误。我还尝试运行alert(e.get message());,由于空间的原因,显然返回了另一个错误。

What are these mysterious methods, and how does a developer call them?

这些神秘的方法是什么,开发人员如何称呼它们?

1 个解决方案

#1


7  

They're property accessors. They're effectively functions that run when you get or set the property.

他们是财产访问者。它们是有效的功能,当您获得或设置属性时运行。

e.message; // getter

e.message = "foobar"; // setter

Using property accessors, these do more than just a simple get and set of the property value. They can run code that was established in the object's property descriptors, so that the property access can have side-effects.

使用属性访问器,这些不仅仅是一个简单的get和set属性值。它们可以运行在对象的属性描述符中建立的代码,以便属性访问可以产生副作用。

Example:

var o = Object.create(Object.prototype, {
    foobar: {
        get: function() { return "getter"; },
        set: function(val) { alert("setter " + val); }
    }
});

o.foobar;  // "getter"
o.foobar = "raboof";  // alerts "setter raboof"

To see the property descriptors set for a given property, use Object.getOwnPropertyDescriptor...

要查看为给定属性设置的属性描述符,请使用Object.getOwnPropertyDescriptor ...

console.dir(Object.getOwnPropertyDescriptor(e, "message"));

Object
    configurable: true
    enumerable: false
    get: function getter() { [native code] }
    set: function setter() { [native code] }

Note that these techniques require an ECMAScript 5 supported implementation.

请注意,这些技术需要ECMAScript 5支持的实现。

#1


7  

They're property accessors. They're effectively functions that run when you get or set the property.

他们是财产访问者。它们是有效的功能,当您获得或设置属性时运行。

e.message; // getter

e.message = "foobar"; // setter

Using property accessors, these do more than just a simple get and set of the property value. They can run code that was established in the object's property descriptors, so that the property access can have side-effects.

使用属性访问器,这些不仅仅是一个简单的get和set属性值。它们可以运行在对象的属性描述符中建立的代码,以便属性访问可以产生副作用。

Example:

var o = Object.create(Object.prototype, {
    foobar: {
        get: function() { return "getter"; },
        set: function(val) { alert("setter " + val); }
    }
});

o.foobar;  // "getter"
o.foobar = "raboof";  // alerts "setter raboof"

To see the property descriptors set for a given property, use Object.getOwnPropertyDescriptor...

要查看为给定属性设置的属性描述符,请使用Object.getOwnPropertyDescriptor ...

console.dir(Object.getOwnPropertyDescriptor(e, "message"));

Object
    configurable: true
    enumerable: false
    get: function getter() { [native code] }
    set: function setter() { [native code] }

Note that these techniques require an ECMAScript 5 supported implementation.

请注意,这些技术需要ECMAScript 5支持的实现。