Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 1.18 KB

PROTOTYPES.md

File metadata and controls

43 lines (33 loc) · 1.18 KB

Prototypes

The functions in this module are to be used for keeping cached references to the built-in prototypes, so that people can't inadvertently break the library by making mistakes in userland.

See sinonjs/sinon#1523

Without cached references

// in userland, the library user needs to replace the filter method on
// Array.prototype
var array = [1, 2, 3];
sinon.replace(array, "filter", sinon.fake.returns(2));

// in a sinon module, the library author needs to use the filter method
var someArray = ["a", "b", 42, "c"];
var answer = filter(someArray, function (v) {
    return v === 42;
});

console.log(answer);
// => 2

With cached references

// in userland, the library user needs to replace the filter method on
// Array.prototype
var array = [1, 2, 3];
sinon.replace(array, "filter", sinon.fake.returns(2));

// in a sinon module, the library author needs to use the filter method
// get a reference to the original Array.prototype.filter
var filter = require("@sinonjs/commons").prototypes.array.filter;
var someArray = ["a", "b", 42, "c"];
var answer = filter(someArray, function (v) {
    return v === 42;
});

console.log(answer);
// => 42