简体   繁体   English

如何在JavaScript中索引原型对象

[英]How to index Prototype objects in JavaScript

So last I tried to index a prototype object (eg: String.prototype ) using a for...in statement I was able to get all its methods and properties. 因此,最后我尝试使用for...in语句for...in原型对象(例如String.prototype )建立索引,从而能够获取其所有方法和属性。

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. 我已经检查了原型是否可以使用Symbol.iterator迭代,但是它仍然没有索引其属性。

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). 我希望能够在JavaScript中为原型对象的每个方法/属性建立索引(我将在一个单独的私有对象中备份它们)。

REASON : 原因

  • It's for a JavaScript library I`m working on: https://github.com/LapysDev/LapysJS . 它用于我正在使用的JavaScript库: 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. 对于防止这样的String.prototype.replace = null情况很有用: String.prototype.replace = null妨碍运行代码。

Built-in prototype methods aren't iterable (see the spec). 内置的原型方法不可迭代(请参见规范)。

Instead, use getOwnPropertyNames() . 而是使用getOwnPropertyNames()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM