为什么报 Uncaught TypeError: console.log(...) is not a function?而另两种方式可以
var x = {
fn: function () {
console.log(this)
// !function () {
// console.log(this)
// }();//正确
(function () {
console.log(this);
})();//Uncaught TypeError: console.log(...) is not a function
}
};
(function () {
console.log(this);
})();//正确
x.fn()
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为第三行的
console.log(this)没有加分号可以简单的运行以下代码,会报同样的错误:
function ()中间的空格去掉和在上面的console 加;
第三行少了分号,导致。
你把你的写法压缩了看就很明白了、圆括号的多重含义由于没写分号被误解了
分号还是不要省,最佳规范都是前辈们的血泪教训。
要么不要省略分号
要么写自动执行函数的时候在其前面加!、;、+、-等符号,以标示这是新的一行