SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript replace method takes functions as arguments

    Hi,
    I am pretty new to javascript.
    I encountered some code in "Javascript the good part" from Douglas Crockford.

    There is a way to passing callback function as second argument to string.replace() method.
    Code:
    var entity = {
        quot: '"',
        lt:   '<',
        gt:   '>'
    };
    var finalStr = '&lt;&quot;&gt;'.replace(/&([^&;]+);/g,
                          function (a, b) {
                              var r = entity[b];
                              return typeof r === 'string' ? r : a;
                          }
                       );

    My questions are:
    1. How do I know what the number of parameters should be in callback function ( function(a,b) ) ?
    2. How do I know what will be passed in as a and b when I am defining the callback function?

    Thanks in advance
    David

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    https://developer.mozilla.org/En/Cor...String/Replace

    If something still isn't clear, let me know.

  3. #3
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •