By iterating over the properties of this
using for..in
you can test against arguments.callee
var o = {
foo: function () {
var k, found, pname = 'unknown';
for (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 that arguments.callee
is forbidden in strict mode by ES5 spec.
In this case you would need a named function expression,
var o = {
foo: function fizzbuzz() {
var k, found, pname = 'unknown';
for (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 you are doing things where you can't guarantee enumerability
for..in
will not necessarily work, take a look here
- If you've changed your
this
, there is no way for the function to know what you want without passing it a reference to your object in another way