Skip to content

Instantly share code, notes, and snippets.

@RienNeVaPlus
Created April 30, 2020 03:14
Show Gist options
  • Save RienNeVaPlus/fee2ee6b3eadf61b79245896357d7624 to your computer and use it in GitHub Desktop.
Save RienNeVaPlus/fee2ee6b3eadf61b79245896357d7624 to your computer and use it in GitHub Desktop.

Revisions

  1. RienNeVaPlus created this gist Apr 30, 2020.
    16 changes: 16 additions & 0 deletions getAllPropertyNames.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    /**
    * Similar to Object.getOwnPropertyNames(obj) but including the properties of the entire prototype chain
    * @param obj
    * @param maxChainLength
    */
    export function getAllPropertyNames(
    obj: { new(): any },
    maxChainLength: number = 10
    ): string[] {
    let set: Set<string> = new Set(), i: number = 0;
    do { i++;
    Object.getOwnPropertyNames(obj).forEach(n => set.add(n));
    obj = Object.getPrototypeOf(obj);
    } while(obj.constructor !== Object && i < maxChainLength);
    return [...set];
    }