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的更多相关文章

  1. js 立即调用函数 IIFE(Immediately Invoked Function Expression) 【转】

    原文链接:https://www.cnblogs.com/ming-os9/p/8891300.html JS中 (function(){...})()立即执行函数   1 (function(){. ...

  2. JS---------IIFE(Imdiately Invoked Function Expression 立即执行的函数表达式)

    +function($){}(jQuery); 今天看到js代码里面有这个格式的代码,不知道啥意思,就去查了一下,我也是js小白.首先前面的+号,这个不是固定非要写+号,只要写一级运算符都可以.目的是 ...

  3. IIFE-js中(function(){…})()立即执行函数写法理解

    介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...

  4. YDKJ 读书笔记 01 Function vs. Block Scope

    Introduction 本系列文章为You Don't Know JS的读书笔记. 书籍地址:https://github.com/getify/You-Dont-Know-JS Scope Fro ...

  5. JS中 (function(){...})()立即执行函数

    (function(){...})() (function(){...}()) 这是两种js立即执行函数的常见写法. 基本概念: 函数声明:function fname(){...}; 使用funct ...

  6. js (function(){}()),(function(){})(),$(function(){});之间的区别

    参考:https://blog.csdn.net/stpice/article/details/80586444 (function(){}()), (function(){})() 均为立即执行函数 ...

  7. js中(function(){}()),(function(){})(),$(function(){});之间的区别

    1. (function(){}())与(function(){})() 这两种写法,都是一种立即执行函数的写法,即IIFE (Immediately Invoked Function Express ...

  8. JavaScript简易教程(转)

    原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...

  9. html、css、js的命名规范

    最佳原则 坚持制定好的代码规范. 无论团队人数多少,代码应该同出一门. 项目命名 全部采用小写方式, 以下划线分隔. 例:my_project_name 目录命名 参照项目命名规则: 有复数结构时,要 ...

随机推荐

  1. JVM内存模型以及垃圾回收

    JAVA堆的描述如下: 内存由Perm和Heap组成.其中Heap = {Old + NEW = { Eden , from, to } } JVM内存模型中分两大块: NEW Generation: ...

  2. hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j ...

  3. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. wxwidget wxpython 可视化开发工具

    wxwidget官方建议的工具集合:http://wiki.wxwidgets.org/Tools 支持wxpython可视化开发工具 wxFormBuilder wxGlade wxDesigner ...

  5. codecombat js

    #1 // Move to the gem. // Don't touch the walls! // Type your code below. this.moveRight(); this.mov ...

  6. jquery中的 $.fn $.fx

    $.fn是指 jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效. 如扩展$.fn.abc() 那么你可以这样子:$("#div").abc(); 常使 ...

  7. 批量替换url,指定内容不替换

    如果需要批量替换url的某几部分,当然是用正则了比如在CI框架中要把 <img src="pc/baidu/aa.jpg"> 替换成 <img src=" ...

  8. phpunit Cannot redeclare class PHPUnit_Runner_Version

  9. offset大家族(一)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 华为S5300系列升级固件S5300SI-V100R005C01SPC100.cc

    这个固件附带了web,注意,这个插件是升级V200的必经固件,所以必须升级为此固件之后才能往下升级. 升级小插曲: 1.升级的使用使用Windows,不要用Mac或者Linux,因为从Mac/Linu ...