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 answers

i'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

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -