javascript - get key name inside of it`s value function -
how key names it`s value nameless function?
what /*some code */ 'foo'
var obj = { foo: function () { console.log('error found in ' + /* code */); } }; obj.foo(); // 'error found in foo'
by iterating on properties of this
using for..in
can test against arguments.callee
var o = { foo: function () { var k, found, pname = 'unknown'; (k in this) if (this[k] === arguments.callee) { found = true; break; } if (found) pname = k; console.log('error found in ' + pname); }, bar: "something else" }; o.foo(); // error found in foo
please note arguments.callee
is forbidden in strict mode es5 spec.
in case need named function expression,
var o = { foo: function fizzbuzz() { var k, found, pname = 'unknown'; (k in this) if (this[k] === fizzbuzz) { found = true; break; } if (found) pname = k; console.log('error found in ' + pname); }, bar: "something else" }; o.foo(); // error found in foo
finally,
- if doing things can't guarantee enumerability
for..in
not work, take here - if you've changed
this
, there no way function know want without passing reference object in way
Comments
Post a Comment