当前位置:主页 > >

JavaScript 报错Uncaught SyntaxError: Unexpected token this

时间:2020-04-13 22:14:42 | 栏目: | 点击:

首先来看一个demo:
function demo(this) {
     console.log(this);
}
本想打印出id为test的input元素对象,结果报错
Uncaught SyntaxError: Unexpected token this
这是什么原因呢?

仔细想了一下,将function中的参数 this 改成其他字符串试试,结果还真的不报错了,程序正常运行。

原来是因为this是JavaScript中的关键字,不能在形参中作为变量名使用。

正确的代码再看一下:
function demo(obj){
   console.log(obj);
}
分享一点小小的知识点,希望能给大家带来大大的方便!

您可能感兴趣的文章:

相关文章