Improve IE Function#name fix and move it to straps.js, since constructor.name is used there too.

Cache result and improve RegExp.
This commit is contained in:
Jürg Lehni 2013-06-12 18:36:18 -07:00
parent 6fe60cff9d
commit 294bfc9c9a
2 changed files with 13 additions and 9 deletions

View file

@ -83,6 +83,19 @@ var Base = new function() {
return _define(obj, name, desc);
};
// Fix Function#name on browsers that do not support it (IE):
if (!(function f() {}).name) {
define(Function.prototype, 'name', {
get: function() {
var name = this.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
// For better performance only parse once, and then cache the
// result through a new accessor for repeated access.
define(this, 'name', { value: name });
return name;
}
});
}
/**
* Private function that injects functions from src into dest, overriding
* (and inherinting from) base.

View file

@ -10,15 +10,6 @@
* All rights reserved.
*/
// First see if Function#name works, and emulate it through an accessor if not.
if (!(function f() {}).name) {
Base.define(Function.prototype, 'name', {
get: function() {
return this.toString().match(/^function\s*(\w*)\s*\(/)[1];
}
});
}
/**
* @name Base
* @class