0

So last I tried to index a prototype object (e.g: String.prototype) using a for...in statement I was able to get all its methods and properties.

But now I seem to be unable to index the prototype as I`ve tried recently.

let stringProto = String.prototype;
for (let i in stringProto)
    console.log(i) // code not running, why? :'(

I've checked if the prototype is iterable using Symbol.iterator and it is but it still does not index its properties.

GOAL

I want to be able to index each method/ property of a prototype object in JavaScript (I`m going to back them up in a separate private object).

REASON:

  • It's for a JavaScript library I`m working on: https://github.com/LapysDev/LapysJS.
  • Useful for preventing edge-cases like this: String.prototype.replace = null from getting in the way of running your code.

1 Answer 1

1

Built-in prototype methods aren't iterable (see the spec).

Instead, use getOwnPropertyNames().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.