简体   繁体   中英

Accessing JavaScript in-memory objects

Is it possible to access JS in-memory objects from within the code? 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. (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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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