Three.js: Function.prototype.name bug in Internet Explorer

Created on 27 Sep 2017  ·  3Comments  ·  Source: mrdoob/three.js

Description of the problem

I'm using THREE in an Aurelia-CLI project. It means it is loaded using modules and RequireJS. It works all fine except in Internet Explorer (tested in IE 11).

The code that breaks is the following (in the polyfill section at the top, lines 42-57) in the /build/three.js file

if ( Function.prototype.name === undefined ) {

  // Missing in IE
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

  Object.defineProperty( Function.prototype, 'name', {

    get: function () {

      return this.toString().match( /^\s*function\s*([^\(\s]*)/ )[ 1 ];

    }

  } );

}

It returns the can't redefine non-configurable property "name" error.

Following this post I've tried to replace the code above with:

if (!(function f() {}).name) {
  Object.defineProperty(Function.prototype, 'name', {
    get: function() {
      var name = (this.toString().match(/^function\s*([^\s(]+)/) || [])[1];
      // For better performance only parse once, and then cache the
      // result through a new accessor for repeated access.
      Object.defineProperty(this, 'name', { value: name });
      return name;
    }
  });
}

And it fixes the problem !

=> It seems that the polyfill code currently used in /build/three.js doesn't work well. Is it possible to replace this code for future builds ?

Three.js version
  • [ ] Dev
  • [x] r87
  • [ ] ...
Browser
  • [ ] All of them
  • [ ] Chrome
  • [ ] Firefox
  • [x] Internet Explorer
OS
  • [ ] All of them
  • [ ] Windows
  • [x] macOS
  • [ ] Linux
  • [ ] Android
  • [ ] iOS
Bug

Most helpful comment

I confirm that only changing the if is enough.

if ( 'name' in Function.prototype === false ) {

it the way to go!

All 3 comments

Would change the check to this make any difference?

if ( 'name' in Function.prototype === false ) {

Or

if ( Function.prototype.hasOwnProperty( 'name' ) === false ) {

I confirm that only changing the if is enough.

if ( 'name' in Function.prototype === false ) {

it the way to go!

Fixed. Thanks!

Was this page helpful?
0 / 5 - 0 ratings