function.call-x
v1.0.0
Published
Function.prototype.call()
Readme
function.call-x
Robustly caches Function.prototype.call for reliable usage across environments.
Installation
npm install function.call-xUsage
const call = require('function.call-x');
function greet(name) {
return `Hello, ${name}!`;
}
// Use call to invoke the function
const result = call.call(greet, null, 'World');
console.log(result); // "Hello, World!"Why?
This module caches a reference to the native Function.prototype.call method. This is useful in scenarios where:
- You need to ensure you're using the original, unmodified
callmethod - Third-party code may have modified or overridden
Function.prototype.call - 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.call method directly.
call.call(func, thisArg, ...args)- func: The function to invoke
- thisArg: The value to use as
thiswhen calling the function - ...args: Individual arguments to pass to the function
Example
const call = require('function.call-x');
const obj = {
value: 42,
getValue: function(multiplier, offset) {
return this.value * multiplier + offset;
}
};
const result = call.call(obj.getValue, obj, 2, 10);
console.log(result); // 94Additional Example
const call = require('function.call-x');
// Using with array methods
const array = [1, 2, 3, 4, 5];
const slice = call.bind(Array.prototype.slice);
const subset = slice(array, 1, 4);
console.log(subset); // [2, 3, 4]
// Using with string methods
const toUpperCase = call.bind(String.prototype.toUpperCase);
console.log(toUpperCase('hello')); // "HELLO"License
MIT
