Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 my instance name?

like image 395
soredive Avatar asked Sep 22 '26 09:09

soredive


1 Answers

There's no general solution.

Objects have no intrinsic knowledge of the out-of-scope variable names to which they've been assigned.

If your variables are in global scope (which they shouldn't be) it's do-able, but only by iterating over the keys in window and finding any whose value matches this.

FWIW (unless you're writing a JS debugger) if you think you need to know this you're probably doing it wrong. JS minifiers for example often change variable names, so you shouldn't rely on them.

like image 59
Alnitak Avatar answered Sep 23 '26 22:09

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!