如何修复 Javascript 中丢失的分号语法错误?

一个朋友为我写了一些代码,其中有一个文件有一个奇怪的语法错误。经过一番搜寻,我将范围缩小到这段代码,这段代码应该会重现错误:

var say = functіon(message) {
alert(message);
return message;
};


say(say("Goodbye!"));

当我运行这个命令时,我在 Internet Explorer 控制台中看到一个错误,它显示的是 SCRIPT1004: Expected ';'。我没有看到任何地方少了一个分号,我也无法想象它要我把一个分号放在哪里。

它期望在哪里使用分号,为什么期望在那里使用分号?

73059 次浏览

You have misspelled the "function" :)

var say = function(message){
alert(message);
return message;
};


say(say("Goodbye!"));

You have inserted functіon :)

I copied your code into jsfiddle, and Chrome too gives an error. I deleted the word "function", and re-typed "function", and it worked fine.

There must be some extra character there.

Your issue is the fact that the i in function is the unicode character i. If you change it to a 'normal' i it should just work.

But now I'm wondering how the hack :) did you get an unicode character there :P

unicode error in js

I've copied and pasted it in my notepad++ and your code look like this in my notepad++, retype your function keyword, i is replaced by ?.

var say = funct?on(message) {
alert(message);
return message;
};
say(say("Goodbye!"));

I had a similar problem and the same error code when debugging someone else's work. To fix this I pasted the section of code into Notepad and then re-copied it back to Visual Studio. The error went away. I think whoever wrote the code originally must have copied it from somewhere with some strange characters in it.

In fact, you inserted unicode "i" instead of normal "i". I get the fellow errors in VSCode:
',' expected. (1, 29)
',' expected. (2, 10)
Declaration or statement expected. (4, 3)
You can try evaluating "functіon" == "function" as well:

function compare() {
return "functіon" === "function"
}
console.log(compare())

However, when I try to compare it by drawing "function" myself: it returns true;

function compare2() {
return "function" === "function"
}
console.log(compare2())

Also, I didn't include semicolons here, in javascript they aren't necessary.

Verify with this page: https://r12a.github.io/uniview/?charlist=funct%D1%96on(message)

It displays information of each character.

enter image description here