[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 ...
随机推荐
- ArrayList,Vector,LinkedList
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 数组列表 和 向量Vector, 都是使用数组方式存储. 向量 使用了 同步synchr ...
- codevs 1462 素数和
1462 素数和 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给定2个整数a,b 求出它们之间(不含a,b)所有 ...
- luoguP4101 [HEOI2014]人人尽说江南好 结论
题目大意: 给定\(n\)堆初始大小为\(1\)的石堆 每次选择两堆石子合并,特别的,合并之后的两堆石子不能\(> m\) 询问先手必赢? 不妨设我们是先手,且最后我们必胜 我们考虑构造局面\( ...
- 【并查集&&带权并查集】BZOJ3296&&POJ1182
bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...
- 记一次帮朋友解决apache站点403错误的过程
apache版本: [root@iZ25eby2utyZ web]# rpm -qa | grep httpd httpd-tools--.el6.centos..x86_64 httpd--.el6 ...
- nginx新建nginx_fzjh.conf文件,不使用默认配置文件
worker_processes 4; events{ worker_connections 1024; } http{ server { listen 80; server_name myserve ...
- Window 下安装
Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择, ...
- ThinkPHP空操作和空控制器的处理
所谓的空操作和空控制器,简而言之就是系统中不存在该方法或控制器,导致系统报错(本文版本3.2.3). 正确URL(测试环境): http://oa.com/index.php/admin/publi ...
- Using PWM Output as a Digital-to-Analog Converter
http://www.ti.com/lit/an/spraa88a/spraa88a.pdf http://www.ti.com/litv/zip/spraa88a The high-resoluti ...
- 用Parallel.For()和Parallel.For<TLocal>()方法实现并行运行迭代
Parallel类是.NET 4中新增的抽象线程类.如果你开发用的是VS2008或更低版本,那么就直接关闭吧,下面两个示例用了匿名委托,如果不知道匿名委托的语法,那么先去简单了解一下,不然很难理解示例 ...