Make _.runInContext not require a fully populated context object.

Former-commit-id: 11bf3cad2bfd3e922e7701eb7b0f48028f522ba6
This commit is contained in:
John-David Dalton
2013-02-27 00:53:12 -08:00
parent eb8953e784
commit b3c550b7c0
8 changed files with 183 additions and 129 deletions

View File

@@ -333,10 +333,8 @@
: partialArgs;
}
if (this instanceof bound) {
// ensure `new bound` is an instance of `bound` and `func`
noop.prototype = func.prototype;
thisBinding = new noop;
noop.prototype = null;
// ensure `new bound` is an instance of `func`
thisBinding = createObject(func.prototype);
// mimic the constructor's `return` behavior
// http://es5.github.com/#x13.2.2
@@ -406,6 +404,20 @@
return func;
}
/**
* Creates a new object that inherits from the given `prototype` object.
*
* @private
* @param {Object} prototype The prototype object.
* @returns {Object} Returns the new object.
*/
function createObject(prototype) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
return result;
}
/**
* A function compiled to iterate `arguments` objects, arrays, objects, and
* strings consistenly across environments, executing the `callback` for each