[Javascirpt] Immediately-Invoked function!!! IMPORTANT
var parkRides = [["Birch Bumpers", 40], ["Pines Plunge", 55],
["Cedar Coaster", 20], ["Ferris Wheel of Firs". 90]]; var fastPassQueus = ["Cedar Coaster", "Pines Plunge", "Birch Bumpers", "Pines Plunge"]; var wantsRide = "Birch Bumpers";
/*Now we store the function in a variable*/
var ticket = buildTicket(allRides, passRides, wantsRide);
ticket(); //call the function, we need a () and ; /*Javascript has an immediately-invoked function*/
var wantsRide = "Cedar Coaster";
//This time we get ride of the ticket variable
//and we just call the buildTicket function without return anything
buildTicket(allRides, passRides, wantsRide);
/**
//What it returns is the whole body of the function only (function(){alert("Quick! You've got a Fast Pass to "+ pass +"!")) //We got the function expression, but we need more to call this
//function expression immediately.
*/ buildTicket(allRides, passRides, wantsRide)();
/**
(function(){alert("Quick! You've got a Fast Pass to "+ pass +"!"))(); //Then it will call the function expression
*/ function buildTicket(allRides, passRides, pick){
if(passRides[0]==pick){
var pass = passRides.shift();
return function(){alert("Quick! You've got a Fast Pass to "+ pass +"!");};
}else{
for(vari = 0; i < allRides.length; i++){
if(allRides[i][0]==pick){
return function(){alert("A ticket is printing for "+ pick + "!\n" +
"Your wait time is about "+ allRides[i][1]+ "minutes.");};
}
}
}
}
Tow ways to call a function expression immediately.
1. var ticket = buildTicket(allRides, passRides, wantsRide);
ticket(); //call the fucntion here!
2. buildTicket(allRides, passRides, wantsRide)(); //Without store function in a variable!!!
-------Example------
function adventureSelector ( userChoice ){
switch(userChoice){
case 1:
return function(){
alert('You\'ve selected the Vines of Doom!'
+ "\n" +
'Hope you have a swingin\' time.');};
case 2:
return function(){
alert('Looks like you want the Lake of Despair!'
+"\n"+
'Watch out for crocs. And I ain\'t talkin\' about shoes.');
};
case 3:
return function(){
alert('The Caves of Catastrophe, really?'
+"\n"+
'Alright, well....nice knowing you.'
);};
default:
break;
}
}
adventureSelector(1)();
[Javascirpt] Immediately-Invoked function!!! IMPORTANT的更多相关文章
- js 立即调用函数 IIFE(Immediately Invoked Function Expression) 【转】
原文链接:https://www.cnblogs.com/ming-os9/p/8891300.html JS中 (function(){...})()立即执行函数 1 (function(){. ...
- JS---------IIFE(Imdiately Invoked Function Expression 立即执行的函数表达式)
+function($){}(jQuery); 今天看到js代码里面有这个格式的代码,不知道啥意思,就去查了一下,我也是js小白.首先前面的+号,这个不是固定非要写+号,只要写一级运算符都可以.目的是 ...
- IIFE-js中(function(){…})()立即执行函数写法理解
介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...
- YDKJ 读书笔记 01 Function vs. Block Scope
Introduction 本系列文章为You Don't Know JS的读书笔记. 书籍地址:https://github.com/getify/You-Dont-Know-JS Scope Fro ...
- JS中 (function(){...})()立即执行函数
(function(){...})() (function(){...}()) 这是两种js立即执行函数的常见写法. 基本概念: 函数声明:function fname(){...}; 使用funct ...
- js (function(){}()),(function(){})(),$(function(){});之间的区别
参考:https://blog.csdn.net/stpice/article/details/80586444 (function(){}()), (function(){})() 均为立即执行函数 ...
- js中(function(){}()),(function(){})(),$(function(){});之间的区别
1. (function(){}())与(function(){})() 这两种写法,都是一种立即执行函数的写法,即IIFE (Immediately Invoked Function Express ...
- JavaScript简易教程(转)
原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...
- html、css、js的命名规范
最佳原则 坚持制定好的代码规范. 无论团队人数多少,代码应该同出一门. 项目命名 全部采用小写方式, 以下划线分隔. 例:my_project_name 目录命名 参照项目命名规则: 有复数结构时,要 ...
随机推荐
- Xshell6和Xftp下载地址,rzsz的使用
官方下载地址:https://cdn.netsarang.net/98f59c09/Xshell-6.0.0076r_beta.exe https://cdn.netsarang.net/98f59c ...
- luoguP5108 仰望半月的夜空 [官方?]题解 后缀数组 / 后缀树 / 后缀自动机 + 线段树 / st表 + 二分
仰望半月的夜空 题解 可以的话,支持一下原作吧... 这道题数据很弱..... 因此各种乱搞估计都是能过的.... 算法一 暴力长度然后判断判断,复杂度\(O(n^3)\) 期望得分15分 算法二 通 ...
- hdu 4435 第37届ACM/ICPC天津现场赛E题
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出N个城市,从1开始需要遍历所有点,选择一 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- 自动化运维工具 ~puppet~
一.模板的应用 到目前为止,资源申报.定义类.声明类等所有功能都只能一个manifest文件中实现,但这却非有效的基于puppet管理IT资源架构的方式.实践 中,一般需要把manifest文件分解成 ...
- 嵌入式设备hacking(转)
原帖地址:http://drops.wooyun.org/papers/5157 0x00 IPCAM hacking TOOLS github-binwalk firmware-mod-kit ID ...
- How to exit the entire application from a Python thread?
If all your threads except the main ones are daemons, the best approach is generally thread.interrup ...
- Namespace declaration statement has to be the very first statement in the script
php 中 Namespace declaration statement has to be the very first statement in the script 错误解决方法: 在PHP文 ...
- iOS中bundle的意义
什么是bundle? bundle就是一个文件夹,按照一定标准组织的目录结构.每个iOS APP至少有一个main bundle,这个main bundle包含了app的二进制代码及任何你用到的资源, ...