What's the difference in call function(s) and call function without "()"?



  • I need to use the method replace for the line. Documentation https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/replace There is an example of the code:

    function replacer(match, p1, p2, p3, offset, string) {
      // p1 - не цифры, p2 - цифры, p3 - не буквы и не цифры
      return [p1, p2, p3].join(' - ');
    }
    var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
    

    We see the function. replacer caused without parameters, although it accepts them in the definition of the function itself! What does that mean? I've written something similar to that of the documentation:

    const regExpression = /(\s+[a-z])/g;
    let strTrimAfterReplace = strTrim.replace(regExpression, replacer);
    

    function replacer(match){
    return match.toUpperCase()
    }

    I don't have a job at all! Please explain that I don't understand or what section of documentation I'm looking at.



  • var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
                                   ^^^^^^^                          ^^^^^^^^
    

    There's only one function in your code - replaceand it's a second parameter. reference by function replacer♪ Afterwards, replacer within the function replace (not you)

    If you'd mentioned it. replacer with brackets like replacer(...)Yeah, you would. Right away. triggered this function, but this is the function. replace I wouldn't have gotten a reference. replacer and the opportunity to call her, output function replacerresulting from arguments You. She gave it to her.

    But in this context it is necessary. reference to the function that the function that receives this reference can itself trigger this function by reference at the time and with the arguments that it considers necessary. This is a very convenient mechanism, widely used in programming and in particular JavaScript



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2