引入的外部js工具包中的语法
一、 代码以及运行结果
1、
(function a(arg1) {
console.log(arg1)
})("x");
2、
(function(arg1) {
console.log(arg1)
})("xx");
3、
(function() {
console.log("1:" + arg1)
}, function b(a) {
console.log("2:" + arg1)
}, function c(arg1, arg2, arg3) {
console.log("3:" + arg1)
console.log("3:" + arg3);
})("xx");
4、
(function(arg1, arg2) {
console.log("first");
arg2();
})("arg1", function() {
console.log("second");
})
5、
var res = (function(arg1, arg2) {
console.log("first");
arg2();
return arg2;
})("arg1", function() {
console.log("second");
return "second";
});
res();
6、
//这个语法会报错,因为函数b的定义域中没有a这个函数
(function a(arg1) {
console.log(arg1);
}, function b(arg1) {
a(arg1);
})("xxx");
二、 结论
1、语法
js的"()()"语法,第一个括号中可以写具名函数也可以写匿名函数,甚至可以写函数集合目的是定义需要调用的函数,第二个()的作用是给第一个括号中的函数提供实参,它还具有调用前面函数的作用,默认调用的是函数集合中的最后一个函数。
2、为什么这么做
首先引入外部js包,需要担心的就是是否会与原有的js中的变量与函数冲突,造成覆盖。故而使用匿名函数放置需要定义的函数,定义函数之后使用([argments...])调用。
双括号语法资料: https://blog.youkuaiyun.com/dataiyangu/article/details/84748259