javascript 无法理解为什么JS代码没有在JS Bin中运行

lfapxunr  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(183)

当我试图运行下面的代码时,在JS Bin中得到下面的错误代码,我做错了什么吗?

let myTodos = {
  day: "Monday",
  meetings: 0,
  meetDone: 0,
}

let addMeeting = function(todo, meet = 0) {
  todo.meetings = todo.meetings + meet;  
}

let meetDone = function (todo, meet = 0) {
  todo.meetDone = todo.meetDone - meet; 

}

let resetDay = function (todo) {
  todo.meetings = 0;
  todo.meetDone = 0;
}

let getSummaryOfDay = function (todo) {
  let meetleft = todo.meetings + todo.meetDone;
  return `You have ${meetleft} meetings for today.!`;
}

addMeeting(myTodos, 4);
addMeeting(myTodos, 2);
meetDone(myTodos, 5);
console.log(getSummaryOfDay(myTodos));
console.log (myTodos);

我在JS Bin中得到的错误如下所示。

"error"
 "SyntaxError: Unexpected token '{'
  at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13924
  at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10866"
h9a6wy2h

h9a6wy2h1#

这是JSBin循环保护中的一个漏洞。
使用此代码(JSBin link)时:

let myTodos = {
  day: "Monday",
  meetings: 0,
  meetDone: 0,
}

let addMeeting = function(todo, meet = 0) {
  todo.meetings = todo.meetings + meet;  
}

let meetDone = function (todo, meet = 0) {
  todo.meetDone = todo.meetDone - meet; 

}

let resetDay = function (todo) {
  todo.meetings = 0;
  todo.meetDone = 0;
}

let getSummaryOfDay = function (todo) {
  let meetleft = todo.meetings + todo.meetDone;
  return `You have ${meetleft} meetings for today.!`;
}

addMeeting(myTodos, 4);
addMeeting(myTodos, 2);
meetDone(myTodos, 5);
console.log(getSummaryOfDay(myTodos));
console.log (myTodos);

JSBin生成将执行的以下文档:

<!DOCTYPE html>

<html>
<head>
  <meta charset=\"utf-8\">
  <meta name=\"viewport\" content=\"width=device-width\">
  <title>JS Bin</title>
<style id=\"jsbin-css\">

</style>
</head>
<body>

<script>try {let myTodos = {
  day: \"Monday\",
  meetings: 0,
  meetDone: 0,
}

let addMeeting = function(todo, meet = 0) {
  todo.meetings = todo.meetings + meet;  
}

let meetDone = function (todo, meet = 0) {
  todo.meetDone = todo.meetDone - meet; 

}

let resetDay = function (todo) {
  todo.meetings = 0;
  todo.meetDone = 0;
}

let getSummaryOfDay = function (todo) {
  let meetleft = todo.meetings + todo.meetDone;
 {
if (window.runnerWindow.protect.prote{;window.runnerWindow.protect.protect({ line: 23, reset: true }); ct({ line: 23 })) break;
 return `You have ${meetleft} meetings for today.!`;
}}

}

addMeeting(myTodos, 4);
addMeeting(myTodos, 2);
meetDone(myTodos, 5);
window.runnerWindow.proxyConsole.log(getSummaryOfDay(myTodos));
window.runnerWindow.proxyConsole.log (myTodos);
} catch (error) { throw error; }

//# sourceURL=xibavicide.js
</script>
</body>
</html>

注意模板文本是如何 Package 在保护代码中的,现在它在语法上是不正确的。
据推测,保护机制是用来阻止无限循环的。
如果您只是删除forJSBin link),则不会触发保护,生成的文档语法正确:

<!DOCTYPE html>

<html>
<head>
  <meta charset=\"utf-8\">
  <meta name=\"viewport\" content=\"width=device-width\">
  <title>JS Bin</title>
<style id=\"jsbin-css\">

</style>
</head>
<body>

<script>try {let myTodos = {
  day: \"Monday\",
  meetings: 0,
  meetDone: 0,
}

let addMeeting = function(todo, meet = 0) {
  todo.meetings = todo.meetings + meet;  
}

let meetDone = function (todo, meet = 0) {
  todo.meetDone = todo.meetDone - meet; 

}

let resetDay = function (todo) {
  todo.meetings = 0;
  todo.meetDone = 0;
}

let getSummaryOfDay = function (todo) {
  let meetleft = todo.meetings + todo.meetDone;
  return `You have ${meetleft} meetings  today.!`;
}

addMeeting(myTodos, 4);
addMeeting(myTodos, 2);
meetDone(myTodos, 5);
window.runnerWindow.proxyConsole.log(getSummaryOfDay(myTodos));
window.runnerWindow.proxyConsole.log (myTodos);
} catch (error) { throw error; }

//# sourceURL=roqosiyasa.js
</script>
</body>
</html>

您可以使用错误中建议的解决方案-在JavaScript区域的任何位置添加// noprotect注解将阻止触发循环保护。JSBin link

wpx232ag

wpx232ag2#

在使用jsbin时从Firefox浏览器切换到Chrome为我修复了这个错误。
如果代码没有任何问题,它在两个浏览器中都运行良好。如果有任何问题,Firefox中的错误原因显示为有问题。Chrome中显示正确的原因。

相关问题