前言

这一章的内容是 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 的两个不同之处

  1. runtime 不同:function declaration 是在浏览器执行代码之前先进行扫描,并创建相应的函数,function expression 是在浏览器执行代码的时候进行创建相应的函数。
  2. function naming 方式不同: function declaration 是将 reference 赋值给函数名,而使用 function expression 不需要 provide a name.

关于第二点,书中最后总结:

  1. 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.
  2. 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.

功能:

  1. Assign a value to a function
  2. Pass a function to a function
  3. 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 可接受一个数字作为参数,通过这个参数的情况进行排序:

  1. >0的时候:place the first item after the second item
  2. =0的时候: leave the items in place
  3. <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的更多相关文章

  1. JavaScript Patterns 3.2 Custom Constructor Functions

    When you invoke the constructor function with new, the following happens inside the function: • An e ...

  2. javascript第十章--Ajax与Comet

    ① XMLHttpRequest对象 ② XMLHttpRequest2级 ③ 进度事件 ④ 跨域源资源共享 ⑤ 其他跨域技术

  3. 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 ...

  4. 理解 JavaScript 回调函数并使用

    JavaScript中,函数是一等(first-class)对象:也就是说,函数是 Object 类型并且可以像其他一等对象(String,Array,Number等)一样使用.它们可以"保 ...

  5. javascript的一点学习

    最近用vue.js用的很爽,在全栈开发的路上一路狂奔,发现后台跟前台一起确实更有意义. 记录一个比较有意思的bug: 目标是对一个全局的paramList进行json格式化显示.代码借鉴了 http: ...

  6. JavaScript Garden

    Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...

  7. 前端工具 - 15个最佳的 JavaScript 表单验证库

    客户端验证在任何项目都是不够的,因为 JavaScript 可以直接忽略,人们可以提交请求到服务器. 然而这并不意味着客户端验证都没必要了,很多时候我们需要在用户提交到服务器之前给予提示.JavaSc ...

  8. 关于JavaScript中的escape、encodeURI和encodeURIComponent

    此文内容与关于JavaScript中的编码和解码函数 关联 escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被 ...

  9. 是什么让javascript变得如此奇妙

    What Makes Javascript Weird...and AWESOME -> First Class Functions -> Event-Driven Evironment ...

随机推荐

  1. windows模糊查询指定进程是否存在

    习惯的查询 wmic process | findStr /i "**" /i 忽略大小写 我查考的链接 常用批处理命令总结3之Find和FindStr

  2. luogu 3790 文艺数学题 - 矩阵树定理 - 容斥原理

    题目传送门 戳我来传送 题目大意 给定一个图,问它的所有生成树的边权的最大公约数之和. 可以考虑计算边权的最大公约数为$i$的生成树的个数$f(i)$,最后累加答案. 然后考虑这样的生成树的个数怎么求 ...

  3. batchGetAnchorLevel(dubbo接口)

    一.编写脚本前的准备工作 1.安装idea,安装本地maven库,并在idea里面配置maven 2.导入git源码(目的在于下载所依赖的基础包)-->File-new-Project from ...

  4. django基础 -- 7.Ajax

    一.ajax 的特点 1.异步交互:客户端发出一个请求后,需要等待服务器响应结束后, 才能发出第二个请求 2.局部刷新:给用户的感受是在不知不觉中完成请求和响应过程. 二.ajax 模板示例 ($.a ...

  5. grub基本应用

    一.基本概念 GRUB(boot  loader):  GRand Unified Bootloader 两个版本: grub .x: grup legacy grub .x: grub2 grub ...

  6. 差分数组|小a的轰炸游戏-牛客317E

    小a的轰炸游戏 题目链接:https://ac.nowcoder.com/acm/contest/317/E 思路  这题考查的是对差分数组原理和前缀和的理解. 四个数组分别记录朝着四个方向下放的个数 ...

  7. 使用JAX-WS开发WebService

    Axis2和CXF是目前流行的Webservice框架,这两个框架都属于重量级框架.相对而言,JAX-WS RI是一个轻量级的框架.开发WebService只需要很简单的几个步骤:写接口和实现-> ...

  8. 第五个神奇的电梯(代码抢先看<1>)

    关于一些自认为比较独特的设计思路,也不知道是好还是坏,放在这里让大家一起看一下. 关于mian函数:因为采用了注册机制所以主函数比较简单. #include "stdafx.h" ...

  9. ETCD应用

    etcd:从应用场景到实现原理的全方位解读 ETCD:A highly-available key value store for shared configuration and service d ...

  10. 【NOI 2016】优秀的拆分

    Problem Description 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 \(A\) 和 \(B\) 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 ...