简体   繁体   English

无法读取未定义的属性“对象”

[英]Cannot read property 'Object' of undefined

I am trying to include a generated js module in my webpage. 我正在尝试在网页中包含一个生成的js模块。 Yet it gives me an error: Uncaught TypeError: Cannot read property 'Object' of undefined at $g["Object"]["freeze"]($env); 但是它给了我一个错误: Uncaught TypeError: Cannot read property 'Object' of undefined $g["Object"]["freeze"]($env); Uncaught TypeError: Cannot read property 'Object' of undefined $g["Object"]["freeze"]($env);

The minimal example is: 最小的示例是:

File index.html : 文件index.html

<html>
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
  </head>
  <body>
    <!-- Note the usage of `type=module` here as this is an ES6 module -->
    <script type="module">

      import { foo } from './foo.js';

      console.log(foo());

    </script>
  </body>
</html>

File foo.js : 文件foo.js

'use strict';
/* Scala.js runtime support
 * Copyright 2013 LAMP/EPFL
 * Author: Sébastien Doeraene
 */

/* ---------------------------------- *
 * The top-level Scala.js environment *
 * ---------------------------------- */

// Get the environment info
var $env = (typeof __ScalaJSEnv === "object" && __ScalaJSEnv) ? __ScalaJSEnv : {};

// Global scope
var $g =
  (typeof $env["global"] === "object" && $env["global"])
    ? $env["global"]
    : ((typeof global === "object" && global && global["Object"] === Object) ? global : this);
$env["global"] = $g;


$env["exportsNamespace"] = void 0;


// Freeze the environment info
$g["Object"]["freeze"]($env);

export function foo() { return "Hello"; }

But if I just copy paste the script into chrome console, then everything works as it should. 但是,如果我只是将脚本复制粘贴到chrome控制台中,那么一切都会正常进行。

You are hitting a bug in Scala.js 0.6.x: https://github.com/scala-js/scala-js/issues/3677 . 您正在Scala.js 0.6.x中遇到一个错误: https : //github.com/scala-js/scala-js/issues/3677 The global detection code in var $g = ... is incorrect when used in ES modules outside of Node.js (eg, in a browser) and fails to discover the correct global object. var $g = ...的全局检测代码在Node.js以外的ES模块中(例如,在浏览器中)使用时不正确,并且无法发现正确的全局对象。

The issue mentions a workaround: add the following <script> tag in your HTML file (before the one that imports the Scala.js code): 问题提到了一种解决方法:在HTML文件中添加以下<script>标记(在导入Scala.js代码的标记之前):

<script type="text/javascript">
var __ScalaJSEnv = { global: window };
</script>

This will explicitly tell Scala.js what the global object is, ie, window . 这将明确告诉Scala.js全局对象是什么,即window

The issue is fixed in Scala.js 1.x, whose RC1 was released two days ago . 该问题已在两天前发布RC1 Scala.js 1.x中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM