patch.js is a simple javascript library for patching object properties withing a specific scope.
You can patch properties on any object using Patch.object. The properties will be replaced with the patched version while the scope is run, and reverted to their original after.
var obj = {
func: function() { return false; },
prop: false
};
Patch.object(obj, {
'func': function() { return true; },
'prop': true
}, function() {
console.log(obj.func()); // --> true
console.log(obj.prop); // --> true
});
console.log(obj.func()); // --> false
console.log(obj.prop); // --> falseIf you are patching a function to return a constant value, you can just provide the value in the patch specification and it will transform it into a function for you.
var obj = {
func: function() { return false; }
};
Patch.object(obj, { 'func': true }, function() {
console.log(obj.func()); // --> true
})
console.log(obj.func()); // --> falseIf you want to temporarily patch all instances of an object, you can of course patch the prototype.
var Obj = function() {
this.value = false;
};
var obj1 = new Obj();
var obj2 = new Obj();
Patch.object(Obj.prototype, { 'value': true }, function() {
console.log(obj1.value); // --> true
console.log(obj2.value); // --> true
});
console.log(obj1.value); // --> false
console.log(obj2.value); // --> falseSometimes you want to patch new object instances created inside the scope while leaving existing and future objects alone. This can be achieved using Patch.new_objects.
var Namespace = {
Obj: function() {
this.value = false;
}
};
var oldObj = new Namespace.Obj();
Patch.new_objects(Namespace, 'Obj', { value: true }, function() {
var newObj = new Namespace.Obj();
console.log(newObj.value); // --> true
console.log(oldObj.value); // --> false
});
console.log(new Namespace.Obj().value); // --> falseNote you need the object the constructor is a property on; unfortunately you can't patch object constructors that are just in the local scope. In the browser, constructors in the global scope are properties on window.
Patch.new_objects(window, 'Date', { 'getTime': 100 }, func_that_creates_dates);