简体   繁体   English

jsfiddle代码有效,但不会运行

[英]Jsfiddle code is valid, but will not run

I revised the code that I had previously had a problem on, fixing all the errors and checking multiple times on different "lint" sites. 我修改了以前遇到问题的代码,修复了所有错误并在不同的“ lint”站点上多次检查。 Just to make sure that the code works in Jsfiddle, I clicked on JShint and was told that all was valid, however, would not run when I click the button to do so. 为了确保代码在Jsfiddle中正常运行,我单击了JShint,并被告知所有内容都是有效的,但是当我单击按钮时,它不会运行。 sample code to create link: 创建链接的示例代码:

    (function (g) { "use strict"; //g = game
g = this[g] = function () {
    var answer;
    //pre game
    answer = prompt(g.msg[0]); //instruction prompt asks if youve played game
    if (answer === 'no') {

etc etc. http://jsfiddle.net/ShiiFtyyMendoza/NsVvU/12/ 等等等等。http://jsfiddle.net/ShiiFtyyMendoza/NsVvU/12/

The error I'm seeing in the console when trying to run this on jsFiddle is : Uncaught TypeError: Cannot set property 'zork_clone' of undefined. 尝试在jsFiddle上运行时,我在控制台中看到的错误是:Uncaught TypeError:无法将属性'zork_clone'设置为undefined。

The string 'zork_clone' only exists in two places in your code; 字符串“ zork_clone”仅存在于代码中的两个位置。 the function name executed in line 95, and the string passed in to your module on line 92. 在第95行执行的函数名称,以及在第92行传递给模块的字符串。

Tracing that parameter in, we see the error thrown from line 2 : g = this[g] = function () { which tells me that this is undefined when that code is executed. 跟踪该参数,我们看到从第2行引发的错误: g = this[g] = function () {这告诉我在执行该代码时this是未定义的。

I believe that in your case this is undefined because you're executing within the scope of a 'self-executing anonymous function' rather than an object declared using the new keyword. 我相信在您的情况下this是未定义的,因为您是在“自执行匿名函数”的范围内执行,而不是在使用new关键字声明的对象内执行。

There are two problems I can see in the page 我可以在页面中看到两个问题

onload event problem 上载事件问题
Your fiddle script is setup to execute after onload event, yet again in your script you are registering another onload handler. 您的小提琴脚本已设置为在onload事件后执行,但再次在脚本中注册了另一个onload处理程序。
To fix this in the left panel of the fiddle under Frameworks & Extensions in second select field select No Wrap - in <body> 在小提琴的左面板中的“ Frameworks & Extensions的第二个选择字段中修复此问题,请No Wrap - in <body>选择“ No Wrap - in <body>

Anonymous function problem 匿名函数问题
From what I can understand you are trying to create a function with variable name(in this case function with name zork_clone ) in the global scope(ie in window ). 据我了解,您正在尝试在全局范围(即window )中创建一个具有变量名称的函数(在本例中为名称为zork_clone函数)。 But you are using strict mode inside the inner function. 但是您正在内部函数中使用strict模式。 Here what you need is this to point to the window object which is the normal case if you are not using strict mode, this changes in the strict mode. 在这里,你需要的是this指向window对象,如果你不使用这是正常的情况下, strict模式,这改变了strict模式。 As you can find here in strict mode this has to be explicit, otherwise it will have the value undefined that is what is happening in your case. 正如您在严格模式下可以找到的那样this必须是明确的,否则它将具有undefined的值,这就是您所遇到的情况。
The fix is to use window global variable instead of using this inside the anonymous function. 解决方法是使用window全局变量,而不是在匿名函数中使用this变量。

Demo: Fiddle 演示: 小提琴

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

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