Module - JavaScript: Good Parts. How is this callback getting its arguments? -


i engaging famous "javascript: parts" douglas crockford. awesome book of course. while, may not have been ready yet, thought of giving shot. need understand following example. second argument in replace() takes 2 arguments a , b. defined? how take value? in advance. did refer stack, don't think helped.

string.method('deentityify', function ( ) {  // entity table. maps entity names // characters.  var entity = { quot: '"', lt: '<', gt: '>' };  // return deentityify method.  return function ( ) {  // deentityify method. calls string // replace method, looking substrings start // '&' , end ';'. if characters in // between in entity table, replace // entity character table.  return this.replace(/&([^&;]+);/g, function (a, b) { var r = entity[b]; return typeof r === 'string' ? r : a;     }    );   }; }( )); 

functions can written accept other functions arguments. such functions called higher-order functions. in example a , b names parameters of function. assigned parameters up-to implementation of replace.

a illustration of idea

var items = [{name:"item1",price:100}, {name:"item2",price:200}];    // lets find object in array, has name "item2"  var result = items.find(function(a){return a.name==="item2"});  console.log(result);

in code function find accepts function determines match-criteria. code of function find iterate array , apply match-criteria function each element until either array ends or first match found. better understanding can change parameter function like:

result = items.find(function(whatever){return whatever.price>=100}); result = items.find(function(whatever){return whatever.price>100}); 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -