Add hasThis to avoid repeatedly checking for this.

Former-commit-id: 15ce8566364ddac60ac01f3a36343c33d9739b77
This commit is contained in:
John-David Dalton
2013-07-18 08:58:40 -07:00
parent 15109f801e
commit 49c032315c
2 changed files with 62 additions and 8 deletions

View File

@@ -1204,6 +1204,34 @@
return result;
}
/**
* Checks if `func` references the `this` keyword.
*
* @private
* @param {Function} func The function to inspect.
* @returns {Boolean} Returns `true` if `this` is referenced, else `false`.
*/
function hasThis(func) {
var result = func.__hasThis__;
if (typeof result == 'boolean') {
return result;
}
result = reThis.test(fnToString.call(func));
defineProperty(func, '__hasThis__', {
'configurable': true,
'enumerable': false,
'writable': true,
'value': result
});
return result;
}
// fallback for older browsers
if (!defineProperty || !reThis) {
hasThis = function() {
return true;
};
}
/**
* A fallback implementation of `isPlainObject` which checks if a given `value`
* is an object created by the `Object` constructor, assuming objects created
@@ -4784,7 +4812,7 @@
return result;
};
}
if (typeof thisArg == 'undefined' || (reThis && !reThis.test(fnToString.call(func)))) {
if (typeof thisArg == 'undefined' || !hasThis(func)) {
return func;
}
if (argCount === 1) {