From 2e82af210c76ca46e7d1fe35fb368e07d76d61ae Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Wed, 23 May 2012 17:39:27 -0500 Subject: [PATCH] Avoid using `Function#bind` when copying a method's `toString` and `valueOf` methods to its wrapped version in class creation. The previous commit fixes the issue in our own polyfill that was causing this error, but we're changing this anyway because people might be relying on the MDC polyfill in the wild. --- src/prototype/lang/class.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/prototype/lang/class.js b/src/prototype/lang/class.js index fa46aa725..0e5726478 100644 --- a/src/prototype/lang/class.js +++ b/src/prototype/lang/class.js @@ -167,9 +167,22 @@ var Class = (function() { value = (function(m) { return function() { return ancestor[m].apply(this, arguments); }; })(property).wrap(method); - - value.valueOf = method.valueOf.bind(method); - value.toString = method.toString.bind(method); + + // We used to use `bind` to ensure that `toString` and `valueOf` + // methods were called in the proper context, but now that we're + // relying on native bind and/or an existing polyfill, we can't rely + // on the nuanced behavior of whatever `bind` implementation is on + // the page. + // + // MDC's polyfill, for instance, doesn't like binding methods that + // haven't got a `prototype` property defined. + value.valueOf = (function(method) { + return function() { return method.valueOf.call(method); }; + })(method); + + value.toString = (function(method) { + return function() { return method.toString.call(method); }; + })(method); } this.prototype[property] = value; }