简体   繁体   English

访问JavaScript内存对象

[英]Accessing JavaScript in-memory objects

Is it possible to access JS in-memory objects from within the code? 是否可以从代码内访问JS内存对象? Are there any internal memory inspectors available? 有内部存储器检查器吗? Can I list the objects with a given prototype (or type) from code? 我可以从代码中列出具有给定原型(或类型)的对象吗?

// EXAMPLE
function Kitten(name) { this.name = name; }
var kitten = new Kitten('furry');
// ...
// Any features like this?
var kittens = ListObjectsOfType(Kitten);
// Or this?
var kittens2 = ListObjectsWithPrototype(kitten.prototype);

Primarily I'm interested in Google's V8 implementations or ES6 (Harmony) specifications. 首先,我对Google的V8实施或ES6(和谐)规范感兴趣。 (I appreciate other technologies too.) (我也很欣赏其他技术。)

You can create a function for this. 您可以为此创建一个函数。 Something like: 就像是:

function ListObjectsOfType(type) {
    var result = [];
    for( var w in window ) {
        var val = window[w];
        if( val instanceof type )
            result.push(val);
    }
    return result;
}

If you invoke this from the Chrome Console, you can plainly inspect/collapse the resulting objects. 如果您是从Chrome控制台调用此功能,则可以检查/折叠生成的对象。 You can extend it to traverse all window vars (you'll want to skip the defaults though). 您可以扩展它以遍历所有窗口变量(尽管您将要跳过默认值)。 I think by definition it is impossible to inspect eg the following: 我认为根据定义,不可能检查以下内容:

function SomeObj() {
    var b = new Kitten('kitty');
}
new SomeObj();

I expect the memory heap to have this obj, but it will not be available/detectable via JS ever. 我希望内存堆具有此obj,但永远不会通过JS使用/检测到它。

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

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