[Javascript] Closure Cove, Common mistake
They’ve got a problem with their existing code, which tries to use a closure. Check it out:
function assignLaser( shark, sharkList ){
var stationAssignment;
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
stationAssignment = function(){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
};
}
}
return stationAssignment;
}
Solution ONE:
Remove stationAssignment here, it will hold the i variable until the loop end, so return the fucntion immediatly when you find the value.
function assignLaser( shark, sharkList ){
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
return function(){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
};
}
}
}
Solution TWO:
var sharkList = ["yrr", "wff", "eff", "gee"];
function makerLaserAssigner(sharkList){
return function(shark){
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
}
}
};
}
var getSharkLaser = makerLaserAssigner(sharkList);
getSharkLaser("eff");
--------------------------------EX-----------------------
The Dev Girls now need a target assignment for each shark. For your reference, the lists of sharks and targets is as follows:
var listOfSharks = ["Sea Pain", "Great Wheezy",
"DJ Chewie", "Lil' Bitey",
"Finmaster Flex", "Swim Khalifa",
"Ice Teeth", "The Notorious J.A.W."];
var listOfTargets = ["icicle bat", "snow yeti",
"killer penguin", "frost tiger",
"polar bear", "iceberg",
"blue witch", "wooly mammoth"];
The Devs want to use the following function call whenever they need to find the right target for any shark:
var getTargetFor = makeTargetAssigner( listOfSharks,
listOfTargets );
getTargetFor("Ice Teeth");
Here’s an example of the pop-up alert that the devs would like their call to getTargetFor to produce:
What up, Ice Teeth!
There've been blue witch sightings in our 'hood!
Time for a swim-by lasering, homie!
*Note: A shark’s list index matches the index of the target it is supposed to eliminate.
YOUR goal is to build out the makeTargetAssigner function with a useful closure, so that it returns a function that can be used in the manner the devs are asking for. To help us check your work more efficiently, use shark as the parameter for a shark’s name in your closure function. You may want to use your own browser’s console to test a few inputs!
Answer:
function makeTargetAssigner( sharks, targets ){
return function(shark){
var s_i = sharks.indexOf(shark);
var t_i = s_i;
var target = targets[t_i];
alert("What up,"+" "+
shark+
"!\n"+
"There've been"+" "+
target+
" sightings in our 'hood!\n"+
"Time for a swim-by lasering, homie!");
};
}
[Javascript] Closure Cove, Common mistake的更多相关文章
- [Javascript] Closure Cove, 1
Returning a function from a function, complete with variables from an external scope, is called a cl ...
- common mistake of closure in loops
[common mistake of closure in loops] 下例中item引用的始终是最后一个值. function showHelp(help) { document.getEleme ...
- 如何理解javascript closure ?
接触过javascript的人应该听过闭包(closure),有一种观点认为是闭包赋予了javascript的强大能力,也赋予了它具备OOP的特征.既然javascript closure如此重要,那 ...
- [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure
function buildCoveTicketMarker(transport){ var passengerNumber = 0; return function(name){ passenger ...
- javascript closure 闭包 事件绑定
先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...
- About javascript closure
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redi ...
- [Javascript] Introducing Reduce: Common Patterns
Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...
- js闭包之初步理解( JavaScript closure)
闭包一直是js中一个比较难于理解的东西,而平时用途又非常多,因此不得不对闭包进行必要的理解,现在来说说我对js闭包的理解. 要理解闭包,肯定是要先了解js的一个重要特性, 回想一下,那就是函数作用域, ...
- javascript closure
http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...
随机推荐
- wpf企业应用之SelectButton(用于列表页之类的选择)
在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中.这里我给自己的控件命名为SelectButton,具体效果见 wpf企业级开发中的几种常见业 ...
- 王彪-20162321《程序设计与数据结构2nd》-第十一周学习总结与实验报告
邻接矩阵实现 例图 分析 变量 需要一个链表来保存数据-即保存结点 需要一个二维数组来保存每个变得权值,有则填入具体数值,没有则用0 定义一个保存边个数的值 函数方法 得到图中边的个数 得到结点的数据 ...
- 2018-2019-2 20162318《网络对抗技术》Exp2 后门原理与实践
一.实验内容 1.使用netcat获取主机操作Shell,cron启动 2.使用socat获取主机操作Shell, 任务计划启动 3.使用MSF meterpreter(或其他软件)生成可执行文件,利 ...
- ROS知识(9)----安装Turtlebot2和远程控制Turtlebot2
安装turtlebot2,场景为:turtlebot2上搭载着一台电脑主机A,该电脑作为主机Master,有自带的电源和3D传感器,roscore在该台机器上启动.pc电脑远程连接A,和A通讯,pc不 ...
- .NET程序员提高效率的70多个开发工具
工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...
- Python3使用setuptools
旧版的setuptools已经不能用于Python3上了,国外有大神fork出了另一分支distribute,它可以支持Python3: 1 2 3 4 5 6 7 #下载 wget http ...
- 使用Redis实现抢购的一种思路(list队列实现)
原文:https://my.oschina.net/chinaxy/blog/1829233 抢购是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如 ...
- Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied) nginx 启动失败
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied) nginx 启动失败 ...
- 转: "SMTP 服务器无法识别在 Mac 2011 Outlook 中发送电子邮件时错误。错误 17092"
在 Mac 2011 Outlook 中发送电子邮件时,您可能会收到下面的错误消息: 无法发送邮件.SMTP 服务器无法识别任何 Outlook 所支持的身份验证方法.请尝试更改帐户设置中的 SMTP ...
- Access control configuration prevents your request from being allo
我发现在在XP环境下我输入网易的220.181.28.42也找不到网页,就跟在LINUX下一样 都揭示: 错误 您所请求的网址(URL)无法获取 --------------------------- ...