JS之匿名函数立即调用模式代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS之匿名函数立即调用模式代码</title>
</head>
<body>
<script>
/************ JS之匿名函数立即调用模式代码 *************/
//普通调用
var a = function(){
console.log(123); //结果:123
};
a();
//立即调用:自己声明自己调用
// function(){
// console.log(123); //会报错,因为不能fanction开头
// }();
//形式一:将方法用括号扩起来
(function(){
console.log(123); //结果:123
})();
//形式二:方法前加(+、-、~、!)
!function(){
console.log(123); //结果:123
}();
//带参数的形式
!function(x){
console.log(x); //结果:abc
}('abc');
</script>
</body>
</html>