use anonymous function for other function's parameter in javascript -



use anonymous function for other function's parameter in javascript -

i'm beginner on javascript. please watch code.

function map(func, ary) { var =0; var array1 = ary; while(ary[i]!=null){ array1[i] = func(ary[i]); i++; } document.write('[' + array1[0] +',' +array1[1]+']'); //just check result } map(function(x) {return (x*4)/2;},[1,3,5,7]);

i want operate "map(function(x) {return (x*4)/2;},[1,3,5,7]);" in javascript.

how can operate code? please help me detail.

i don't understand want ask. code works. utilize null, false, true instead of null false true. , aware document.write() bad way of displaying result.

better ways are:

create/get div, span, p or elem , utilize this: div.innerhtml = result; console.log(result); // press f12 open firebug/web tools

.

// map function returns result: function map(func, ary) { var = 0; var result = []; // empty array results while(ary[i] != null){ result[i] = func(ary[i]); i++; } homecoming result; // must homecoming result } // objects {} , arrays[] passed reference functions function map(func, ary) { var = 0; while(ary[i] != null){ ary[i] = func(ary[i]); // changing original `ary` i++; } // no need homecoming }

javascript anonymous-function

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -