function.apply-x
v1.0.0
Published
Function.prototype.apply()
Readme
function.apply-x
Robustly caches Function.prototype.apply for reliable usage across environments.
Installation
npm install function.apply-xUsage
const apply = require('function.apply-x');
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}
// Use apply to call the function with an array of arguments
const result = apply.call(greet, null, ['Hello', 'World']);
console.log(result); // "Hello, World!"Why?
This module caches a reference to the native Function.prototype.apply method. This is useful in scenarios where:
- You need to ensure you're using the original, unmodified
applymethod - Third-party code may have modified or overridden
Function.prototype.apply - You want to protect against prototype pollution
- You're building low-level utility libraries that need guaranteed native behavior
API
The module exports the cached Function.prototype.apply method directly.
apply.call(func, thisArg, argsArray)- func: The function to invoke
- thisArg: The value to use as
thiswhen calling the function - argsArray: An array of arguments to pass to the function
Example
const apply = require('function.apply-x');
const obj = {
value: 42,
getValue: function(multiplier, offset) {
return this.value * multiplier + offset;
}
};
const result = apply.call(obj.getValue, obj, [2, 10]);
console.log(result); // 94License
MIT
