Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

JavaScript/Closures/Exercises

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Topic: Closure 1. Write a script that creates a parameterizable function decoratorFactory. It accepts a string that 'decorates' logging information with the given parameter. E.g.: const tiny = decoratorFactory("Log message from subroutine 'someTinyAction': "); alert(tiny("Division by zero."));. The script shall internally use a closure. 100% "use strict"; function decoratorFactory(someText) { // 'someText' is known in 'decoratorFunction' as well as in 'logFunction' const logFunction = function (message) { // use 'someText' from the outer lexical environment and the function's parameter 'message' return(someText + ": " + message) // maybe: plus timestamp, ... } return logFunction; } const computeMsg = decoratorFactory("Log message from subroutine 'computeAverage'"); alert(computeMsg("Division by zero.")); const main = decoratorFactory("*** Log message from 'main'"); alert(main("No database connection.")); alert(main("Unknown system error."));