Three.js: Internet Explorer 中的 Function.prototype.name 错误

创建于 2017-09-27  ·  3评论  ·  资料来源: mrdoob/three.js

问题描述

我在 Aurelia-CLI 项目中使用了三个。 这意味着它是使用模块和 RequireJS 加载的。 除了在 Internet Explorer 中(在 IE 11 中测试)之外,它一切正常。

中断的代码如下(在顶部的 polyfill 部分,第 42-57 行)在/build/three.js文件中

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 ];

    }

  } );

}

它返回can't redefine non-configurable property "name"错误。

这篇文章之后,我尝试将上面的代码替换为:

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;
    }
  });
}

它解决了这个问题!

=> 目前在 /build/three.js 中使用的 polyfill 代码似乎不能很好地工作。 是否可以为将来的构建替换此代码?

三.js版本
  • [ ] 开发
  • [x] r87
  • [ ] ...
浏览器
  • [ ] 他们都是
  • [ ] 铬合金
  • [ ] 火狐
  • [x] 浏览器
操作系统
  • [ ] 他们都是
  • [ ] 窗户
  • [x] macOS
  • [ ] Linux
  • [ ] 安卓
  • [ ] iOS

最有用的评论

我确认只更改if就足够了。

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

这是要走的路!

所有3条评论

将支票改成这样会有什么不同吗?

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

或者

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

我确认只更改if就足够了。

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

这是要走的路!

固定的。 谢谢!

此页面是否有帮助?
0 / 5 - 0 等级