How to get instance name using jquery

in my.js

$(document).ready(function(){
    a = new person();
    b = new person();
        ...
})

person.prototype.init = function(){..}

function person(){
    var someVariable;
    function doSomeThing(){
        ...
        // I need my instance name
        ...
    }
    return {doSomeThing:doSomeThing}
}

how can i get the name of my instance?

+3
source share
1 answer

There is no general solution.

Objects do not have built-in knowledge about variable names outside the scope of which they were assigned.

If your variables are in the global scope (which they should not be), they are executed, but only by iterating over the keys in windowand searching for any values ​​that match this.

FWIW (if you are not writing a JS debugger), if you think you need to know this, you are probably mistaken. For example, JS minifiers often change variable names, so you should not rely on them.

+3
source

All Articles