如何使用Javascript的(attr在这里)与咖啡 [英] How to use Javascript's for (attr in this) with Coffeescript
问题描述
在Javascript中,for(attr in this)通常是危险的使用...我同意。这是我喜欢Coffeescript的一个原因。但是,我在Coffeescript编程,有一个case,我需要Javascript的for(attr在这)。在Coffeescript中有很好的方法吗?
In Javascript, the "for (attr in this)" is often dangerous to use... I agree. That's one reason I like Coffeescript. However, I'm programming in Coffeescript and have a case where I need Javascript's "for (attr in this)". Is there a good way to do this in Coffeescript?
我现在做的是在嵌入式原始Javascript中编写一堆逻辑,例如:
What I am doing now is writing a bunch of logic in embedded raw Javascript, such as:
...coffeescript here...
for (attr in this) {
if (stuff here) {
etc
}
}
推荐答案
而不是
for own attr, value of this
if attr == 'foo' && value == 'bar'
console.log 'Found a foobar!'
编译: https://gist.github.com/62860f0c07d60320151c
它接受无论是键和值在循环中,这是非常方便。您可以在之后插入
自己的
关键字,以便强制执行 if对象.hasOwnProperty(attr)
检查哪个应该从原型中过滤出你不想要的东西。
It accepts both the key and the value in the loop, which is very handy. And you can insert the own
keyword right after the for
in order to enforce an if object.hasOwnProperty(attr)
check which should filter out anything from the prototype that you don't want in there.
这篇关于如何使用Javascript的(attr在这里)与咖啡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!