简体   繁体   English

Coffeescript。 for / in循环

[英]Coffeescript . for/in loop

Anybody knows if it is possible to get a 有人知道是否有可能得到一个

javascript for/in loop javascript for / in循环

from coffeescript? 来自coffeescript?

Actually would like to write the js function 其实想写js函数

function logobject(o) {
   for (p in o)
     console.log(p + "=" + o[p])
}

in coffeescript. 在coffeescript。

console.log "#{k}=#{v}" for k, v of o

This might be a bit confusing for CoffeeScript newbies, but the for..in loop is used to iterate over arrays, while the for..of loop is used to iterate over objects. 对于CoffeeScript新手来说,这可能有点令人困惑,但for..in循环用于迭代数组,而for..of循环用于迭代对象。

logobject = (o) ->
  console.log key + "=" + value for key, value of o

Also, to restrict this to own properties of the object (skips inherited properties via hasOwnProperty()), the "own" keyword can be added: 此外,要将此限制为对象的属性(通过hasOwnProperty()跳过继承的属性),可以添加“own”关键字:

for own key, value of o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM