JavaScript: how to define an enumerable method without using Object.defineProperty?

I want to add an Object method, but now it has all the arrays and objects. When I use for(.. in ..) , it is re-read, and this is a problem for my software. So, I need to make my method non-enumerable.

I know that there is Object.defineProperty() , but it is not supported by the old browser (which still exists) and even the latest versions of Konqueror.

Is there any other way to make the method non-enumerable?

+4
source share
2 answers

No. That is why he believed that bad practice uses JavaScript for..in without immediately checking for hasOwnProperty . Ie you should always do:

 for (var item in obj) { if (obj.hasOwnProperty(item)) { // ... } } 

Tools like JSLint report an error in your code if you use for..in without checking hasOwnProperty .

+3
source

For ECMAScript 3 (used in the old browser) you cannot change the enumerable attribute.
If you want to filter out this method, perhaps you can check the type of the in (.. in ..) object.

0
source

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


All Articles