how to get instance name using jquery
in my.js
$(document).ready(function(){
a = new person();
b = new person();
...
})
person.prototype.init = function(开发者_JS百科){..}
function person(){
var someVariable;
function doSomeThing(){
...
// I need my instance name
...
}
return {doSomeThing:doSomeThing}
}
how can I get my instance name?
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.
精彩评论