如何从其值无名函数中获取关键名称?
什么是/ *某些代码* /得到'foo'
_connectivityDialog.Closing += (sender, args) =>
{
args.Cancel = true;
};
答案 0 :(得分:1)
通过使用this
迭代for..in
的属性,您可以针对arguments.callee
进行测试
case class B [A <: ABase] (someField: A) {
type ThisA = this.someField.A
def compare1(that: B [A]): Int = someField.compare(that.someField.asInstanceOf[ThisA])
def compare2(that: B [A]): Int = someField.compare(that.someField.asInstanceOf[ThisA])
def compare3(that: B [A]): Int = someField.compare(that.someField.asInstanceOf[ThisA])
}
请注意,ES5规范禁止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
严格模式。
在这种情况下,您需要一个命名函数表达式
arguments.callee
最后,
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
不一定有效,请查看here for..in
,则该功能无法在不向其传递对象的引用的情况下知道您想要的内容