Skip to content Skip to sidebar Skip to footer

Javascript: Alert Object Name As A String

I'm trying to alert any JavaScript object as a string, in a function. This means if the parameter given to the function is window.document, the actual object, it should alert 'wind

Solution 1:

This is not possible to do in a self contained script.

If using a preprocessor would be an option, then you could write one which converts example(whatever) into example('whatever'). Other than that I'm afraid you're out of luck.

Solution 2:

The first problem is that objects don't have names.

The second problem is that from your examples, you're not really wanting to print the (nonexistent) name of an object, you want to print the expression that evaluated into a reference to an object. That's what you're trying to do in this example:

example(document.getElementById('something'));

For that to print document.getElementById('something'), JavaScript would have had to keep the actual text of that expression somewhere that it would make available to you. But it doesn't do that. It merely evaluates the parsed and compiled expression without reference to the original text of the expression.

If you were willing to quote the argument to example(), then of course it would be trivial:

example( "document.getElementById('something')" );

Obviously in this case you could either print the string directly, or eval() it to get the result of the expression.

OTOH, if you want to try a real hack, here's a trick you could use in some very limited circumstances:

functionexample( value ) {
    var code = arguments.callee.caller.toString();
    var match = code.match( /example\s*\(\s*(.*)\s*\)/ );
    console.log( match && match[1] );
}

functiontest() {
    var a = (1);
    example( document.getElementById('body') );
    var b = (2);
}

test();

This will print what you wanted:

document.getElementById('body')

(The assignments to a and b in the test() function are just there to verify that the regular expression isn't picking up too much code.)

But this will fail if there's more than one call to example() in the calling function, or if that call is split across more than one line. Also, arguments.callee.caller has been deprecated for some time but is still supported by most browsers as long as you're not in strict mode. I suppose this hack could be useful for some kind of debugging purposes though.

Solution 3:

Don't know why you need this, but you can try walking the object tree recursively and compare its nodes with your argument:

functionobjectName(x) {

    functionsearch(x, context, path) {
        if(x === context)
            return path;
        if(typeof context != "object" || seen.indexOf(context) >= 0)
            return;
        seen.push(context);
        for(var p in context) {
            var q = search(x, context[p], path + "." + p);
            if(q)
                return q;
        }
    }

    var seen = [];
    returnsearch(x, window, "window");
}

Example:

console.log(objectName(document.body))

prints for me

window.document.activeElement

Post a Comment for "Javascript: Alert Object Name As A String"