前言

这一章的内容是 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. Guitar Pro怎样可以快速打出三连音?

    相信很多的音乐爱好初学者在使用入门编曲软件Guitar Pro的时候,都会碰到这样一个问题,很多乐谱中都出现了三连音,那么我们使用Guitar Pro的时候,如何能够打出三连音呢? 三连音,就是三等分 ...

  2. XcodeProj,使用Ruby更改工程文件

    利用xcodeproj修改xcode工程文件 一,Ruby基础 Ruby迭代器each.map.collect.inject each——连续访问集合的所有元素collect—-从集合中获得各个元素传 ...

  3. 关于no matching key exchange method found. Their offer: diffie-hellman-group1-sha1的解决办法

    原文链接:https://mycyberuniverse.com/error/no-matching-key-exchange-method-found-openssh7.html What caus ...

  4. c 语言中宏定义和定义全局变量的区别

    宏定义和定义全局变量的区别: 1 作用时间不同. 宏定义在编译期间即会使用并替换,而全局变量要到运行时才可以. 2 本质类型不同. 宏定义的只是一段字符,在编译的时候被替换到引用的位置.在运行中是没有 ...

  5. Newcoder 华华给月月出题(线筛)题解

    题目描述: 华华刚刚帮月月完成了作业.为了展示自己的学习水平之高超,华华还给月月出了一道类似的题: Ans=⊕Ni=1(iNmod(109+7))Ans=⊕i=1N(iNmod(109+7)) ⊕⊕符 ...

  6. ActiveMQ安装使用

    入门: https://www.cnblogs.com/cyfonly/p/6380860.html http://www.uml.org.cn/zjjs/201802111.asp https:// ...

  7. C# 递归模型定义。赋值

    https://blog.csdn.net/wumiju/article/details/80429412 public partial class ProductTypeModel:DbEntity ...

  8. SQL 多行合并一行

    select stuff((select ',' + CONVERT(VARCHAR(50),id)+'' from tab_menu  group by id for xml  path('')), ...

  9. (转)Paper list of Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning

    Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning 2018-08-03 19:16:56 本文转自:http ...

  10. JAVA 上传文件到linux上并解压缩

    package com.inborn.inshop.controller.mkt; import java.io.*; import ch.ethz.ssh2.ChannelCondition;imp ...