What are these mysterious javascript exception methods?

I studied javascript exception in Google Chrome.

enter image description here

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

What are these mysterious methods and what does the developer call them?

+6
source share
1 answer

They are accessories for objects. They work efficiently when you get or set a property.

 e.message; // getter e.message = "foobar"; // setter 

Using property accessors, they do more than just get and set the value of the property. They can run code that has been set in the property descriptors of an object, so accessing properties can have side effects.

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 this property, use 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 methods require support supported by ECMAScript 5.

+7
source

Source: https://habr.com/ru/post/921715/


All Articles