A simple

You'll implement once, a function that takes another function as an argument, and returns a new version of that function that can only be called once.

Subsequent calls to the resulting function should have no effect (and should return undefined).

For example:

logOnce = once(console.log)

logOnce("foo") // -> "foo"

logOnce("bar") // -> no effect

once = (fn) ->
 called = false
 (args...) ->
   unless called
     called = true
     fn(args...)
function once(fn) {
  var call = true
  return function() {
    if (call) {
      call = false
      return fn.apply(this, arguments)
    }
  }
}
posted @ 2014-07-30 00:47  强子~Developer  阅读(199)  评论(0编辑  收藏  举报