获取它的值函数内的关键名称

时间:2016-01-27 13:16:34

标签: javascript object

如何从其值无名函数中获取关键名称?

什么是/ *某些代码* /得到'foo'

_connectivityDialog.Closing += (sender, args) =>
            {
                args.Cancel = true;
            };

1 个答案:

答案 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,则该功能无法在不向其传递对象的引用的情况下知道您想要的内容