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 ...
随机推荐
- ps -ef|grep详解
ps命令将某个进程显示出来 grep命令是查找 中间的|是管道命令 是指ps命令与grep同时执行 PS是LINUX下最常用的也是非常强大的进程查看命令 grep命令是查找,是一种强大的文本搜索工具, ...
- py4CV例子3Mnist识别和ANN
1.什么是mnist数据集: , , ], ,,,,,,,,]], ., ., , , ], ,,,,,,,,]], ., ., , , ])) animals_net.setTermCriteri ...
- Codeforces 37D Lesson Timetable - 组合数学 - 动态规划
题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...
- VC++ 使用CreateProcess创建新进程
https://www.cnblogs.com/fancing/p/6477918.html #include <windows.h> #include <tchar.h> # ...
- 【python014--字符串内置函数】
一.实现跨越多行的字符串 1.采用双引号实现 str1 = ""待我长发及腰,将军归来可好?此身君子意逍遥,怎料山河萧萧天光乍破遇,暮雪白头老寒剑默听奔雷,长枪独守空豪醉卧沙场君莫 ...
- 网络 --- 4 socketserver模块并发 连接合法性
一.socketserver模块 ②服务端 ③客户端 二.连接合法性 ①os.urandom(n) 一种bytes类型的随机生成n个字节字符串的方法 而且每次生成的值都不相同.再加上md5 ...
- springmvc配置文件<context:component-scan>
在spring.xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的 ...
- Selenium android driver
selenium android « s « Jar File Download http://www.java2s.com/Code/Jar/s/selenium-android.htm How t ...
- sudo中的 各类授权 名称包含的操作 权限命令?
sudo是 do something as super user: 或者说: as Super User Do something: 就是 为 "非根用户赋予根用户的权限" 使用 ...
- 振兴中华|2013年蓝桥杯A组题解析第三题-fishers
标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振 我做起振兴 做起振兴中 起振兴中华 比赛 ...