JavaScript 第十章总结:first class functions
前言
这一章的内容是 advanced knowledge and use of functions. 讲了关于 function 的使用的一些特殊的方面。
function expression 的定义
格式:
var fly = function(num){
for(var i = 0; i < num; i++) {
console.log("Flying!");
}
};
fly(3);
与 function declaration 的两个不同之处
- runtime 不同:function declaration 是在浏览器执行代码之前先进行扫描,并创建相应的函数,function expression 是在浏览器执行代码的时候进行创建相应的函数。
- function naming 方式不同: function declaration 是将 reference 赋值给函数名,而使用 function expression 不需要 provide a name.
关于第二点,书中最后总结:
- When the browser evaluates a function declaration, it creates a function as well as a variable with the same name as the function, and stores the function reference in the variable.
- When the browser evaluates a function expression, it creates a function, and it's up to you what to do with the function reference.
使用 function 的三种特殊的情况
定义
first class values: function 可以作为 value 来使用,书中的描述如下:
First class: a value that can be treated like any other value in a programming language, including the ability to be assigned to a variable, passed as an argument, and returned from a function.
功能:
- Assign a value to a function
- Pass a function to a function
- Return a function from a function
无论是 function declaration 还是 function expression, 它们的名字都是函数的 reference 值,因此可以利用、传递这个值来 invoke 一个 function.
1. Assign the value to a function
格式:var fly = [function expression]
2.Pass the function to a function
格式:function boo(aFunction){
aFunction("boo");
}
function fun(echo){
}
boo(fun);
3. Return a function from a function
使用 sort() 这个 method
sort() 介绍:
sort 可接受一个数字作为参数,通过这个参数的情况进行排序:
- >0的时候:place the first item after the second item
- =0的时候: leave the items in place
- <0 的时候:place the first item before the second item
总结的两种情况:
可以使用一个自定义的 compare(num1,num2) 函数,来得到上面所要求的参数,并且分下面两种情况:
- ascending:if(num1>num2){return 1;}或者 return(num1-num2)
- descending:if(num1<num2){return 1;}或者 return(num2-num1)
JavaScript 第十章总结:first class functions的更多相关文章
- JavaScript Patterns 3.2 Custom Constructor Functions
When you invoke the constructor function with new, the following happens inside the function: • An e ...
- javascript第十章--Ajax与Comet
① XMLHttpRequest对象 ② XMLHttpRequest2级 ③ 进度事件 ④ 跨域源资源共享 ⑤ 其他跨域技术
- What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?
What are the benefits to using anonymous functions instead of named functions for callbacks and par ...
- 理解 JavaScript 回调函数并使用
JavaScript中,函数是一等(first-class)对象:也就是说,函数是 Object 类型并且可以像其他一等对象(String,Array,Number等)一样使用.它们可以"保 ...
- javascript的一点学习
最近用vue.js用的很爽,在全栈开发的路上一路狂奔,发现后台跟前台一起确实更有意义. 记录一个比较有意思的bug: 目标是对一个全局的paramList进行json格式化显示.代码借鉴了 http: ...
- JavaScript Garden
Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...
- 前端工具 - 15个最佳的 JavaScript 表单验证库
客户端验证在任何项目都是不够的,因为 JavaScript 可以直接忽略,人们可以提交请求到服务器. 然而这并不意味着客户端验证都没必要了,很多时候我们需要在用户提交到服务器之前给予提示.JavaSc ...
- 关于JavaScript中的escape、encodeURI和encodeURIComponent
此文内容与关于JavaScript中的编码和解码函数 关联 escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被 ...
- 是什么让javascript变得如此奇妙
What Makes Javascript Weird...and AWESOME -> First Class Functions -> Event-Driven Evironment ...
随机推荐
- linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法
内网的linux服务器给开发员用来测试以及共享文件使用,今天早上发现xshell连接不上该服务器,一开始进入系统显示reboot and select proper boot device or in ...
- ldap集成nginx
nginx版本:1.10.2 nginx安装: wget http://nginx.org/download/nginx-1.10.2.tar.gz tar zxvf nginx-1.10.2.tar ...
- topcoder srm 545 div1
problem1 link 这个可以贪心地从前向后构造.假设当前已经的字符串为$S$,对于一个字符$c$来说,设将$c$加到$S$后得到的新串为$S^{'}$.那么如果$X+Y+Z \ge minIn ...
- Restful framework【第一篇】RESTful 规范
什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类审 ...
- win10中命令操作Zookeeper
目录 zk客户端命令: 连接: 命令: 四字命令: 常用命令: 返回参数说明: 参考: zk客户端命令: 连接: C:\Users\qhong\Desktop $ zkCli.cmd -server ...
- nowcoder 合并回文子串
链接:https://www.nowcoder.com/acm/contest/6/C来源:牛客网题目输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc&q ...
- P4492 [HAOI2018]苹果树
思路 题目要求的其实就是每种方案的权值之和(因为每种方案的概率相等) 所以自然想到要求所有的边对最终答案的贡献次数 考虑这一条边被经过了多少次,有这个子树内的点数*子树外的点数次,即\(k\times ...
- P3273 [SCOI2011]棘手的操作
吐槽 上午风浔凌julao问我的神题 操作又多又毒瘤又棘手... 然后bzoj题号正好是2333,2333333333 思路 貌似只有我是这么写的 线段树合并, 每个线段树存每个连通块的信息,维护点的 ...
- p3302 [SDOI2013]森林(树上主席树+启发式合并)
对着题目yy了一天加上看了一中午题解,终于搞明白了我太弱了 连边就是合并线段树,把小的集合合并到大的上,可以保证规模至少增加一半,复杂度可以是\(O(logn)\) 合并的时候暴力dfs修改倍增数组和 ...
- 《机器学习实战》之k-近邻算法(改进约会网站的配对效果)
示例背景: 我的朋友海伦一直使用在线约会网站寻找合适自己的约会对象.尽管约会网站会推荐不同的人选,但她并不是喜欢每一个人.经过一番总结,她发现曾交往过三种类型的人: (1)不喜欢的人: (2)魅力一般 ...