Sunday, 15 February 2015

javascript - logOnce higher order function -


I want to implement a function that takes another function as argument, and a new version of that function Which can only be called once.

The first thing does, but the second does not work.

Why does another function not work, but anyhow a function like this one inside the first one?

  var logon = once (console.log) function once (fn) {var call = true; Return work (word) {if (call) {call = false; Return FN (word); }}} Function once (fn) {var call = true; If (call === true) {call = false; Return FN; }} Logon ("foo"); ---- & gt; "Foo" logon ("blue"); ---- & gt; "Blue"  
= "post-text" itemprop = "text">

Your second method does not work because it's the same fn . In fact, this is equivalent to

  function (fn) {return fn; }  

Therefore, one time (console.log) is just console.log , and you can call it

The first approach works because you return a different function, which will be the original one or call on the basis of a value.


No comments:

Post a Comment