oop - Function as an argument to a function in Javascript -
oop - Function as an argument to a function in Javascript -
this question has reply here:
function in javascript can called once 15 answersi'm trying implement function takes function argument, , returns new version of function can called once.
subsequent calls resulting function should have no effect (and should homecoming undefined).
for example:
logonce = once(console.log) logonce("foo") // -> "foo" logonce("bar") // -> no effect
you can utilize flag on function obeject passing argument
class="snippet-code-js lang-js prettyprint-override">function once(func){ homecoming function(){ if(!func.performed){ func.apply(this,arguments); func.performed = true; } } } var logonce = once(console.log); logonce("test 1"); logonce("test 2");
javascript oop
Comments
Post a Comment