迭代coffeescript中的方法名称

时间:2013-01-17 11:45:15

标签: coffeescript

在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制作它?

1 个答案:

答案 0 :(得分:0)

'秘密'是在使用对象时使用of命令:

console.log i for i of MyClass.prototype
相关问题