【问题标题】:iterating over method names in coffeescript [duplicate]迭代咖啡脚本中的方法名称[重复]
【发布时间】:2012-12-31 22:47:09
【问题描述】:

在 JS 中,我可以这样做:

for(i in MyClass.prototype) {
  console.log(i);
}

它会显示方法名称。没关系。

现在,如果我用 coffeescript 做这个:

for i in MyClass.prototype
  console.log i

它将被编译为:

var i, _i, _len, _ref;

_ref = MyClass.prototype;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  i = _ref[_i];
  console.log(i);
}

但是原型没有length 属性,所以它坏了。

我怎样才能用coffeescript制作它?

【问题讨论】:

标签: coffeescript


【解决方案1】:

“秘诀”是在使用对象时使用of命令:

console.log i for i of MyClass.prototype

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多