Robust Call-Bound JavaScript Intrinsics

1.0.4 · active · verified Sun Apr 19

call-bound is a utility library that provides robust, call-bound versions of JavaScript intrinsic functions, ensuring they work correctly even if `Function.prototype.call` or `Function.prototype.bind` are removed or modified from the global scope. It achieves this by internally leveraging `call-bind` and `get-intrinsic` to fetch and bind the original intrinsic methods securely. The current stable version is 1.0.4. This package is part of a suite of libraries by @ljharb focused on shims and polyfills, often released on an as-needed basis rather than a strict time-based cadence, with updates typically driven by bug fixes, security patches, or new ECMAScript features. Its key differentiator is its resilience against prototype pollution, making it vital for libraries that need to rely on core JavaScript functionality without fear of tampering in hostile environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to obtain a robust, call-bound intrinsic function and verifies its functionality even after simulated tampering with global prototypes, showcasing its resilience.

const assert = require('assert');
const callBound = require('call-bound');

// Get a robust, bound version of Array.prototype.slice
const slice = callBound('Array.prototype.slice');

// Simulate a hostile environment where intrinsics might be tampered with
delete Function.prototype.call;
delete Function.prototype.bind;
delete Array.prototype.slice;

// The call-bound slice still works, unaffected by the deletions
const originalArray = [1, 2, 3, 4, 5];
const slicedArray = slice(originalArray, 1, -1); // Should be [2, 3, 4]

assert.deepStrictEqual(slicedArray, [2, 3, 4]);
console.log('Array.prototype.slice (call-bound) works correctly:', slicedArray);

// Example with Object.prototype.hasOwnProperty
const hasOwnProperty = callBound('Object.prototype.hasOwnProperty');
const obj = { a: 1, b: 2 };

assert.strictEqual(hasOwnProperty(obj, 'a'), true);
assert.strictEqual(hasOwnProperty(obj, 'c'), false);
console.log('Object.prototype.hasOwnProperty (call-bound) works correctly.');

view raw JSON →