A javascript issue for closure -
given next code:
string.method('deentityify', function () { var entity = { quot: '"', lt: '<', gt: '>' }; homecoming function () { homecoming this.replace(/&([^&;]);/g, function (a, b) { var r = entity[b]; homecoming typeof r === 'string' ? r : a; } ); }; }()); document.write('deentityify: ' + '<">'.deentityify() + '<br>');
regarding the
function (a, b) { var r = entity[b]; homecoming typeof r === 'string' ? r : a; }
how come anonymous function parameter value a, b? of course of study have tried, output right. can can help me?
the function argument 'replace' call. regex matches passed function parameters. write code way, look:
function match(a, b) { var r = entity[b]; homecoming typeof r === 'string' ? r : a; } var result = this.replace(/&([^&;]);/g, match)
the names of parameters (a & b) inconsequential , like. first parameter matched value , subsequent parameters values of matched groups. clarity function written as:
function matchfn(match, group1, group2, group3) { var r = entity[group1]; homecoming typeof r === 'string' ? r : match; }
to quote mdn
a function invoked create new substring (to set in place of substring received parameter #1). arguments supplied function described in "specifying function parameter" section below.
javascript closures
No comments:
Post a Comment