javascript反射查找原型方法、全局范围方法和对象

发布于 2024-12-05 00:36:33 字数 63 浏览 13 评论 0原文

如何找到使用反射定义的原型方法(不是 PrototypeJS)?另外,如何查找全局范围内所有已定义的对象和方法?

How can I find the prototype methods (not PrototypeJS) that have been defined using reflection? Also, how to find all defined objects and methods in the global scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-12-12 00:36:33

for...in 沿着原型链一路向下,它不保证任何特定的顺序,但它应该循环遍历您正在寻找的所有属性...

for ( var property in obj ) {
    //obj[property];
}

如果您是寻找继承(通过原型)成员,添加hasOwnProperty()检查...

for ( var property in obj ) {
    if ( ! obj.hasOwnProperty(property) ) {
        //obj[property] is an inherited property...
    }
}

另外,我从未尝试过这个,但是使用window< /code>,我相信你会找到你正在寻找的东西 为了...

for ( var property in window) {
    //window[property];
}

the for...in travels all the way down the prototype chain, it doesn't garauntee any specific order, but it should loop through all the properties you're looking for...

for ( var property in obj ) {
    //obj[property];
}

If you are looking for only inherited (via prototype) members, add a hasOwnProperty() check...

for ( var property in obj ) {
    if ( ! obj.hasOwnProperty(property) ) {
        //obj[property] is an inherited property...
    }
}

also, I've never tried this but, but using window, I believe you'd find what you are looking for...

for ( var property in window) {
    //window[property];
}
风渺 2024-12-12 00:36:33

要迭代所有原型属性(包括继承的属性):

var prototypeData = Object.getPrototypeOf(soomething);
for(var key in prototypeData) {
    // prototypeData[key] is a prototype value
}

要查看所有定义的对象,请以相同的方式迭代 window

for(var key in window) {
    // window[key] is a globally defined value
}

To iterate over all prototype properties (including inherited ones):

var prototypeData = Object.getPrototypeOf(soomething);
for(var key in prototypeData) {
    // prototypeData[key] is a prototype value
}

To see all defined objects, iterate over window the same way:

for(var key in window) {
    // window[key] is a globally defined value
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文