1

class SomeClass {
  myFunction() {
    console.log('Hello, World');
  }
}

for (let obj in SomeClass.prototype) {
  console.log(obj);
}

Hi everyone, I am trying to loop over class's prototype but it's not working, it doesn't give an error loop just won't execute, I tried running this code in dev tools same situation, what's wrong here, how do I loop over prototype object?

David
  • 208,112
  • 36
  • 198
  • 279
  • 2
    It's working exactly as intended - it loops over all enumerable properties. Of which there are none. With that said, it seems like a bit of [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - what are you trying to achieve that requires going through the prototype properties? – VLAZ Sep 21 '22 at 18:02
  • I am writing some custom decorators and using reflect metadata npm package to wire up some decorators. – Muhammad Sami Sep 21 '22 at 18:14
  • The OP could make use of e.g. [`Object.getOwnPropertyNames`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) – Peter Seliger Sep 21 '22 at 20:16

0 Answers0