这是...?
这是函数调用与定义于一体的写法 这种叫做匿名函数
你可以试试这句(function(){alert(arguments[0])})("test")
前面的括号是函数的定义 定义一个匿名函数 这个定义体可作为函数名使用
后面的括号就是传参数啦 传入一个名为window的变量
(function (window) {
'use strict';
// Your starting point. Enjoy the ride!
})(window);
(function (Vue) { // 依赖全局vue
var app = new Vue({
el: "#todoapp"
})
})(Vue)
立即执行:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>立即执行</title>
</head>
<body>
<div id="app">
<h2>立即执行函数:</h2>
<p>{{ func }}</p>
</div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
(function(e){
alert(e);
alert("我是立即执行函数")
})("canshu") // 这个可以为空,或者传入参数
new Vue({
el: "#app",
data: {
func: "(function() { alert('立即执行这个函数') })()"
}
})
</script>
</html>
每天搞懂一点点,都TM忘了(我学过吗?_)
网友评论