Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to traverse all the non–enumerable properties?

Tags:

javascript

When writing JavaScript, I often forget some properties of built-in objects and have to look them up in mdn, which is quite bothering since it slows down my work.

Instead of referring to documents, It is more convenient to create an object and use for ... in to inspect it with console.log(). But when it comes to non–enumerable properties, even for ... in won't help.

So my question is, besides google and documents, is there any way to inspect non–enumerable properties?

for(var i in Object){
    console.log([i,Object[i]]);
    // ["wtbind", function()]
}
console.log(Object.hasOwnProperty('create'));
// true
// Here Object.create is a non–enumerable property,
// and I have to look it up in documents if I forget it.
like image 877
Rufus Avatar asked Oct 27 '25 06:10

Rufus


2 Answers

This

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

seems useful, although it is a rather recent addition and apparently doesn't work in Opera. Although it only lists own properties, you can always climb up the prototype chain.

like image 176
Imp Avatar answered Oct 28 '25 20:10

Imp


I know this is old, but you can use new features for ES6 and I think ES2018 for this:

You can use a for...of with the help of an iterator if you implement an iterable object (since object are not iterable)

Let's say you have the following object with one enumerable property and on non-enumerable property

const obj = Object.defineProperties({}, {
  enumerableKey: {
    enumerable: true,
    value: 'hello'
  },
  nonEnumerableKey: {
    enumerable: false,
    value: 'world'
  }
});

You can make it iterable using Symbol.iterator and define your own implementation. In the example below, I'm yielding the value of each key (but you can yield whatever you want)

obj[Symbol.iterator] = function* () {
  for(const key of Object.getOwnPropertyNames(this)) {
    yield this[key]
  }
}

After this, you can traverse it with a for...of loop:

// This will log:
// 'hello'
// 'world'
for(const val of obj) {
  console.log(val);
}

Or use the spread operator (since spreading uses iteration via Symbol.iterator)

[...obj] //[ 'hello', 'world' ]

Of you'd like to read more about it, I would recommend you read the following:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
like image 23
cafesanu Avatar answered Oct 28 '25 20:10

cafesanu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!