Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

javascript - 无法读取未定义的属性“对象”(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控制台中,那么一切都会正常进行。)   ask by Ford O. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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中修复。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...