From 2162baa6c0a942e0abbcc17a2f8144f403330998 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 28 Feb 2013 02:49:26 -0800 Subject: [PATCH] Add `contextProps` to make `_.runInContext` work in older IEs. Former-commit-id: 208f25296d35ed29a283fb1598de0052a5a2703d --- lodash.js | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/lodash.js b/lodash.js index 1298be31e..ea827e880 100644 --- a/lodash.js +++ b/lodash.js @@ -61,8 +61,15 @@ /** Used to match unescaped characters in compiled string literals */ var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g; + /** Used to assign default `context` object properties */ + var contextProps = [ + 'Array', 'Boolean', 'Date', 'Function', 'Math', 'Number', 'Object', 'RegExp', + 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN', 'parseInt', + 'setImmediate', 'setTimeout', 'toString' + ]; + /** Used to fix the JScript [[DontEnum]] bug */ - var shadowed = [ + var shadowedProps = [ 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf' ]; @@ -122,7 +129,7 @@ * @returns {Function} Returns the `lodash` function. */ function runInContext(context) { - context = context ? _.extend(createObject(window), context) : window; + context = context ? _.defaults({}, context, _.pick(window, contextProps)) : window; /** Native constructor references */ var Array = context.Array, @@ -467,9 +474,9 @@ ' <% if (hasDontEnumBug) { %>\n\n' + ' var ctor = iterable.constructor;\n' + ' <% for (var k = 0; k < 7; k++) { %>\n' + - " index = '<%= shadowed[k] %>';\n" + + " index = '<%= shadowedProps[k] %>';\n" + ' if (<%' + - " if (shadowed[k] == 'constructor') {" + + " if (shadowedProps[k] == 'constructor') {" + ' %>!(ctor && ctor.prototype === iterable) && <%' + ' } %>hasOwnProperty.call(iterable, index)) {\n' + ' <%= loop %>\n' + @@ -633,7 +640,9 @@ } if (this instanceof bound) { // ensure `new bound` is an instance of `func` - thisBinding = createObject(func.prototype); + noop.prototype = func.prototype; + thisBinding = new noop; + noop.prototype = null; // mimic the constructor's `return` behavior // http://es5.github.com/#x13.2.2 @@ -714,7 +723,6 @@ * top - A string of code to execute before the iteration branches. * loop - A string of code to execute in the object loop. * bottom - A string of code to execute after the iteration branches. - * * @returns {Function} Returns the compiled function. */ function createIterator() { @@ -725,7 +733,7 @@ 'isKeysFast': isKeysFast, 'nonEnumArgs': nonEnumArgs, 'noCharByIndex': noCharByIndex, - 'shadowed': shadowed, + 'shadowedProps': shadowedProps, // iterator options 'arrays': 'isArray(iterable)', @@ -757,20 +765,6 @@ ); } - /** - * 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