Question: I am writing some code to extend `Object`, and I would like to loop over the methods within the object. When I output the value of `this.constructor`, I see the methods that I would like (in the example below I see `test`). However, when I loop over the methods using a for, it only displays the method `getType`. How can I get just the methods of `class A`?

```js
Object.prototype.getType = function () {
  // Outputs correct class and methods
  console.log(this.constructor)

  for (let i in this.constructor) {
    // Outputs wrong methods
    console.log(i)
  }
}

class A {
  test() {console.log('hello')}
}

new A().getType()
```

Answer: 
Two problems:

1. Properties can be enumerable – visited by for…in loops – or not. When you define a method in a `class`, the default is non-enumerable. (`Object.prototype.getType`, on the other hand, is enumerable because that’s the default when you create a property by assignment.)
2. You’re looping over properties of what the object claims is its constructor, but non-static methods like `test` are properties of the constructor’s `prototype` property (`A.prototype`), not the constructor itself (`A.test` isn’t a thing).

You can find non-enumerable properties with the [`Object.getOwnPropertyNames`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) function:

```
Object.prototype.getType = function () {
  for (let name of Object.getOwnPropertyNames(this.constructor.prototype)) {
    console.log(name)
  }
}

```

Note that this only includes *own properties* – properties directly on the object – so if you want to include inherited methods, you’ll have to go up the prototype chain.

```
function* getAllPropertyNames(obj) {
  do {
    yield* Object.getOwnPropertyNames(obj)
  } while (obj = Object.getPrototypeOf(obj))
}

Object.prototype.getType = function () {
  for (let name of getAllPropertyNames(this.constructor.prototype)) {
    console.log(name)
  }
}

```

And if you want to include properties directly on the object, you’ll have to start there (and hope `this.constructor.prototype` is `Object.getPrototypeOf(this)`).

```
Object.prototype.getType = function () {
  for (let name of getAllPropertyNames(this)) {
    console.log(name)
  }
}

```

Also, don’t extend `Object.prototype`. *Especially* not with enumerable properties. Management is not responsible for dependencies that break in mysterious ways when you do this.
Perhaps not the most elegant solution, but one way to do this is via a call to `Object.getOwnPropertyNames()`, passing the object instances `__proto__` reference.

```js
Object.prototype.getType = function () {

  /*
  // Outputs correct class and methods
  console.log(this.constructor)

  for (let i in this.constructor) {
    // Outputs wrong methods
    console.log(i)
  }
  */

  for(let j of Object.getOwnPropertyNames(this.__proto__)
  .filter(method => method !== 'constructor')) 
  {
    // Outputs properties of prototype, without constructor method
    console.log(j);
  }

}

class A {
  test() {console.log('hello')}
}

new A().getType()
```