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. 转 TCP/IP的三次握手与四次挥手详解

    TCP((Transmission Control Protocol)传输控制协议,是一个面向连接的协议.在运用此协议进行数据传输前都会进行连接的建立工作(三次握手):当数据传输完毕,连接的双方都会通 ...

  2. Codeforces Beta Round #37 B. Computer Game 暴力 贪心

    B. Computer Game 题目连接: http://www.codeforces.com/contest/37/problem/B Description Vasya's elder brot ...

  3. Codeforces Beta Round #11 B. Jumping Jack 数学

    B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...

  4. Codeforces Round #131 (Div. 1) A - Game

    A. Game time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  5. 记一次完整的pc前端整站开发

    我所做的项目是一个线上的旅游平台,当然不是大家耳熟能详的那些旅游平台了,项目刚开始也没有必要去评价它的好坏,在这里我就不多说了~这是线上地址有兴趣的同学可以去看看(www.bookingctrip.c ...

  6. [Visual Studio] 安装清单

    VS安装位置要求路径必须是英文,且位于Program Files (x86)文件夹下. 下载工具vs_Professional.exe:https://pan.baidu.com/s/1jHRjiia ...

  7. 关于多重嵌套的JSON数据解析

    最近项目中需要封装一套复杂的数据模型返回给前端,大致就是一个用户会有多笔订单,每个订单下可能会有多笔保单, 大致的数据模型如下: 为了方面描述,先看一下一个用户下有一条订单,一条订单下有一个保险订单的 ...

  8. cocos2d-x项目101次相遇:使用触摸事件移动 精灵

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...

  9. .net 基于Jenkins的自动构建系统开发

    先让我给描述一下怎么叫一个自动构建或者说是持续集成 : 就拿一个B/S系统的合作开发来说,在用SVN版本控制的情况下,每个人完成自己代码的编写,阶段性提交代码,然后测试-修改,最后到所有代码完工,进行 ...

  10. LeetCode152:Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...