/    Sign up×
Community /Pin to ProfileBookmark

How know what object property the function was called from?

If I create an object and create a property “alerter” and set that equal to a function, is there any way to get that my function was called from a property named alerter?

EXAMPLE:

[CODE]
function func()
{
//how would I alert the property this function is attached to?
alert( “Called by: ” + ??? );
}
document.getElementById( “something” ).alerter = func;
document.getElementById( “somethingelse” ).anotherProp = func;
[/CODE]

how would I do this?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 26.2009 — Use the [B]this[/B] keyword to refer to the object which owns the function. As far as I know, you cannot grab the name of the property to which the function has been assigned.
Copy linkTweet thisAlerts:
@rnd_meMay 27.2009 — If I create an object and create a property "alerter" and set that equal to a function, is there any way to get that my function was called from a property named alerter?
[/QUOTE]

you can now.
[CODE]

function whoAmI(fn, that){
for(var i in that){
if(that[i]===fn){
return i;
}
}
} // end whoAmI()


this.fred = function john(){
var myName = whoAmI(arguments.callee, this);
return myName;
}

fred()//==="fred"

[/CODE]


you inspired me.

i was going to say no, you can't; that's what i already knew, when i had this idea.

it dawned on me that while it's not a property, it can be found. i wrote the simple logic into a re-usable function.

as long as it's used on objects, and you don't conflict the property names, it should work perfectly.

use "whoAmI(arguments.callee, this)" without modification to return the current function's property name.

if you give your function statement a name (like john in this example) you can use it instead of the long-winded arguments.callee for more compact code.

in this case, it would be whoAmI(john, this), and it would still return "fred".

"john" is not available to anything outside of the function itself, so you could give all your bound functions the "name" x.

you could then use whoAmI(x, this) anytime you need to find the property name.

adventurous coders can pass something besides [I]this [/I]as a second argument.

no refunds.
×

Success!

Help @6tr6tr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...