Created
August 27, 2009 16:20
-
-
Save fearphage/176409 to your computer and use it in GitHub Desktop.
Defines several additional methods for Opera missing from JS1.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(slice, toString) { | |
| // opera bug - DSK-259939 | |
| var arrayMethodsFailOnStrings = Array.prototype.filter.call('fPaAiSlS', function(c) { | |
| return 'fail'.indexOf(c) == -1; | |
| }).join('') != 'PASS'; | |
| function addGenerics(klass, methods) { | |
| methods.forEach(function(name) { | |
| if ((typeof klass.prototype[name] == 'function') && !klass[name]) { | |
| klass[name] = (function(fn, isArray) { | |
| return function(obj) { | |
| return fn.apply( | |
| (arrayMethodsFailOnStrings && isArray && (toString.call(obj) == '[object String]') | |
| ? obj.split('') | |
| : obj | |
| ) | |
| ,slice.call(arguments, 1) | |
| ); | |
| }; | |
| })(klass.prototype[name], klass == Array); | |
| } | |
| }); | |
| } | |
| addGenerics(Array, 'every filter forEach map reduce reduceRight slice some'.split(' ')); | |
| addGenerics(String, 'replace substr'.split(' ')); | |
| if (!('name' in Function.prototype)) { | |
| Function.prototype.__defineGetter__('name', function() { | |
| return this.toString().match(/function\s+([^\s\(]+)/) ? RegExp.$1 : 'anonymous'; | |
| }); | |
| Function.prototype.__defineSetter__('name', function(readonly) { }); | |
| } | |
| })(Array.prototype.slice, Object.prototype.toString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment