[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 目录命名 参照项目命名规则: 有复数结构时,要 ...
随机推荐
- BZOJ 3091: 城市旅行 lct 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...
- 【Python3】【树形dp】uva10253 Series-Parallel Networks
设“共n个叶子,且每个非叶节点至少有两个子节点”的树的数量为f[n],再乘2就是本题答案. 设状态d(i,j)表示每棵子树最多包含i个叶子.一共有j个叶子的树的个数.于是f(n)=d(n-1,n).假 ...
- WinlogonHack获取系统密码
实验环境: win03 sp1 Gina.dll与Msgina.dll Gina.dll在NT/2000中交互式的登陆支持是由WinLogon调用Gina.dll实现的,Gina.dll提供了一个交互 ...
- 设置Nginx开机自启动
Nginx 是一个很强大的高性能Web和反向代理服务器.虽然使用命令行可以对nginx进行各种操作,比如启动等,但是还是根据不太方便.下面介绍在linux下安装后,如何设置开机自启动. 首先,在lin ...
- Xcode 中的IOS工程模板
1.IOS模板主要分为: Application .Framework.Other application 分为:Master-Detail Application 可以构建树形导航模式引用,生成的代 ...
- Pyhton 编程风格
博文概要:1.Python语句和语法2.Python标识符3.基本编程风格 一.Python语句和语法 1.可以从一行的任何地方开始2.续行 \: ''':闭合操作符,单一语句跨多行 3.代码组 .缩 ...
- mysqld 多线程 用pstree -p 显示
http://blog.chinaunix.net/uid-22566367-id-3751084.html http://blog.csdn.net/fly2nn/article/details/6 ...
- Java clone方法(下)
1.终于调用的是一个JNI方法,即java本地方法,加高速度 2.使用clone方法,分为浅复制.深复制,这里直接使用网上抄来的案例来说明吧: 说明: 1)为什么我们在派生类中覆盖Object的clo ...
- 编码原则:必须使用的 TODO
结构 // TODO:JS ParentId 不要使用硬编码. var parentId = record.get('ParentId'); var parentNode = me.getStore( ...
- .NET:CLR via C# The Interlocked Anything Pattern
Many people look at the Interlocked methods and wonder why Microsoft doesn't create a richer set of ...