npm.io
1.0.0 • Published 9 months ago

function.apply-x

Licence
MIT
Version
1.0.0
Deps
0
Size
2 kB
Vulns
0
Weekly
0

function.apply-x

Robustly caches Function.prototype.apply for reliable usage across environments.

Installation

npm install function.apply-x

Usage

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 apply method
  • 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 this when 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); // 94

License

MIT

Keywords