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 ...
随机推荐
- android之自定义弹框
step1 创建窗体 final AlertDialog dialog =new Builder(this).create(); step2 获取View View viewDialog =View. ...
- centos-6.5安装部署LNMP环境
安装部署前,确保安装了gcc和gcc-c++ 系统信息: [root@zww ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@ ...
- 牛客网数据库SQL实战(11-15)
11.获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'.结果第一列给出当前员工的emp_no,第二列给出其manager ...
- C# Math类简介运用
总结了一下几个常用的Math类 /* ######### ############ ############# ## ########### ### ###### ##### ### ####### ...
- Spring核心简介
Spring简介 Spring是一个开源.轻量级框架.在诞生之初,创建Spring的主要目的是用来替代更加重量级的企业级Java技术,尤其是EJB(Enterprise JavaBean).从最初的挑 ...
- 如何创建并运行java线程 , 多线程使用
http://www.importnew.com/20672.html https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblo ...
- 【AI】微软人工智能学习笔记(一)
数据分析平台 01|数据平台概况图示 上面图中所示就是微软人工智能数据平台的相关的技术. 02.1| Cortana Intelligence Suite 从上面图中可以看到, 其中有一个Cortan ...
- Kibana——日志可视化工具
Kibana 基础入门 kibana产品介绍 Kibana :是一个开源的分析和可视化平台,旨在与 Elasticsearch 合作.Kibana 提供搜索.查看和与存储在 Elasticsearch ...
- maven web项目生成WebContent或WebRoot目录
本文为博主原创,转载请注明出处: 新建maven web工程时,自动生成的文件结构目录如下: 这个是maven web自动生成的目录结构,我想让其生成如java web工程的WebRoot 或WebC ...
- JavaScript(2)
JavScript在页面上显示时间,首先我们先来了解关于时间的一些简单方法: getFullYear() 获取当前年份,getMonth() 0-n(一月到十二月),getDate() 1-31(月 ...