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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!