Question
Define a function decorator in javascript. addPermissions(oldFn) Parameters: oldFn - a function to modify / decorate Returns: a function - this new, returned function has
Define a function decorator in javascript.
addPermissions(oldFn)
Parameters:
- oldFn - a function to modify / decorate
Returns:
- a function - this new, returned function has one extra parameter more than oldFn (the function passed in), and this parameter determines whether or not oldFn should be run
Description:
This decorator takes an old function and returns a new function with an extra parameter. The extra parameter, an object that represents whether or not the function is allowed to run, is in the beginning of the parameter list. If this object has a property named admin, and it's value is exactly true, then the old function, oldFn, is run, and the result of calling oldFn is returned. If the object does not have an admin property exactly equal to true (or if the object cannot have properties, objects like undefined and null), then the oldFn is not run, and undefined is returned instead.
Example:
const myParseInt = addPermissions(parseInt); // create a new version of parseInt console.log(myParseInt(null, '101', 2)); // undefined console.log(myParseInt({admin: true}, '101', 2)); // 5 console.log(myParseInt(undefined, '101', 2)); // undefined console.log(myParseInt(5, '101', 2)); // undefined
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started