一 参考文献
  《JavaScript忍者秘籍》
 
二 函数特征总结
  1. 函数是【第一型对象(first-class object)】:可以像这门语言的其它对象一样使用
    函数可以共处,可以将其视为其它任意类型的JavaScript对象
    普通的JavaScript数据类型,函数可以被任意变量进行引用,
    或者声明成对象字面量,甚至可将其作为函数参数进行传递。
    ①可以通过【字面量】[创建]
    ②可以[赋值]给【变量】、【数组】或【其他对象的属性】
    ③可以作为一个独立实体的【参数】[传递]给函数(见样例1)。
    ④可以作为【函数的返回值】进行[返回]
    ⑤可以拥有[动态创建并赋值]的【属性
    ⑥可以在【任何地方】[创建]函数(即 只要能使用本门语言表达式的地方,就能创建函数,包括:匿名函数、即时函数等)
  2. 浏览器的事件轮询
    ①事件相互穿插发生。
      浏览器事件[页面加载完成、页面卸载]、
      网络事件
      用户事件[鼠标点击、移动、按键]、
      计时器事件
    ②浏览器的事件轮询是【单线程】的。
    ③每个事件都在自己的生命周期中进行处理,所有其他事件必须等到这个事件处理结束后才能继续处理。
 
  3. 回调概念
    定义:
       ①当我们定义一个(回调)函数稍后执行,无论何时定义、或者何处执行(在浏览器执行海曙其他地方执行)
      ②定义一个函数,以便其他一些代码在适当的时机再回头调用它(>回调函数)。
      ③可以将函数作为参数,传递给另一个函数,并在随后对该函数进行调用。
      样例1
function useless(callback){
return callback();
} var text = 'Johnny';
console.log(useless(function(){ return text; }) == text);//true 
      样例2
var count = 1;
var MAX_COUNT = 2;
function A(messageA,fn1,arg1){
console.log("A:",messageA);
C("A call C");
fn1(arg1);
setTimeout(B("A call B once again after 1000ms"), 1000);//A再次回调B,二者形成了双向沟(调)通(用)
}
function B(messageB,fn2){ //启动入口:B(" startup from B");
console.log("B:",messageB);
if(count < MAX_COUNT){
count++;
A("B call A",function(msg){
console.log(msg);
},"B call A's others arg/function");
}
}
function C(messageC,fn3){
console.log("C:",messageC);
B("C call B");
}

test/output:

B("Startup from B");
B: Startup from B
A: B call A
C: A call C
B: C call B
B call A's others arg/function
B: A call B once again after 1000ms

  4.函数声明

  格式:function [名称可选]([arg1,arg2,...,argN]) { ... }

    []表示:可选,非必须

  5.作用域和函数

    意义:当我们声明一个函数时,不仅要关注该函数可用的作用域,还要关注该函数自身所创建的作用域,以及函数内部的声明是如何影响这些作用域的。

    特征:

      1.在JavaScript中,作用域是由function进行声明的,而不是代码块【()、{}等】

         比如:if(...) {  } //并不能像其他语言一样形成作用域

      2.变量声明的作用域开始于声明的地方结束于所在函数的结尾,与代码嵌套无关。

      3.机制提升:命令函数的作用域是指声明该函数的整个函数范围,与代码嵌套无关。

      4.对于作用域声明全局上下文就像一个包含页面所有代码的超大型函数

function outer(){
assert(typeof outer == 'function',"1 outer() is in scope"); //true
assert(typeof inner == 'function',"2 inner() is in scope"); //true
assert(typeof a == 'number',"3 a is in scope");
assert(typeof b == 'number',"4 b is in scope");
assert(typeof c == 'number',"5 c is in scope"); var a = 1;
function inner(){
assert(typeof inner == 'function',"6 inner() is in scope"); //true
assert(typeof a == 'number',"7 a is in scope"); //true
assert(typeof b == 'number',"8 b is in scope"); //true
assert(typeof c == 'number',"9 c is in scope"); //true
}
var b = 2;
if(a == 1){
assert(typeof a == 'number',"10 a is in scope"); //true
assert(typeof b == 'number',"11 b is in scope"); //true
assert(typeof c == 'number',"12 c is in scope");
var c= 3;
}
assert(typeof a == 'number',"13 a is in scope"); //true
assert(typeof b == 'number',"14 b is in scope"); //true
assert(typeof c == 'number',"15 c is in scope"); //true
inner();
} outer(); assert(typeof outer == 'function',"16 outer() is in scope"); //true
assert(typeof inner == 'function',"17 inner() is in scope");
assert(typeof a == 'number',"18 a is in scope");
assert(typeof b == 'number',"19 b is in scope");
assert(typeof c == 'number',"20 c is in scope");

output:

1 outer() is in scope
2 inner() is in scope
3 a is in scope
4 b is in scope
5 c is in scope
10 a is in scope
11 b is in scope
12 c is in scope
13 a is in scope
14 b is in scope
15 c is in scope
6 inner() is in scope
7 a is in scope
8 b is in scope
9 c is in scope
16 outer() is in scope
17 inner() is in scope
18 a is in scope
19 b is in scope
20 c is in scope

s

JavaScript之深入理解【函数】的更多相关文章

  1. 理解和使用 JavaScript 中的回调函数

    理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报  分类: JavaScript(4)    目录( ...

  2. [转]理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  3. 【JavaScript】理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  4. JavaScript callback function 回调函数的理解

    来源于:http://mao.li/javascript/javascript-callback-function/ 看到segmentfault上的这个问题 JavaScript 回调函数怎么理解, ...

  5. 理解javascript中的回调函数(callback)【转】

    在JavaScrip中,function是内置的类对象,也就是说它是一种类型的对象,可以和其它String.Array.Number.Object类的对象一样用于内置对象的管理.因为function实 ...

  6. 深入理解JavaScript执行上下文、函数堆栈、提升的概念

    本文内容主要转载自以下两位作者的文章,如有侵权请联系我删除: https://feclub.cn/post/content/ec_ecs_hosting http://blog.csdn.net/hi ...

  7. 理解与使用Javascript中的回调函数 -2

    在javascript中回调函数非常重要,它们几乎无处不在.像其他更加传统的编程语言都有回调函数概念,但是非常奇怪的是,完完整整谈论回调函数的在线教程比较少,倒是有一堆关于call()和apply() ...

  8. 理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  9. JavaScript大杂烩2 - 理解JavaScript的函数

    JavaScript中的字面量 书接上回,我们已经知道在JavaScript中存在轻量级的string,number,boolean与重量级的String,Number,Boolean,而且也知道了之 ...

  10. 《JavaScript高级程序设计》读书笔记(三)基本概念第六小节理解函数

    内容---语法---数据类型---流程控制语句 上一小节---理解函数 本小节 函数--使用function关键字声明,后跟一组参数以及函数体 function functionName(arg0, ...

随机推荐

  1. C# -- 等待异步操作执行完成的方式

    C# -- 等待异步操作执行完成的方式 1. 等待异步操作的完成,代码实现: class Program { static void Main(string[] args) { Func<int ...

  2. WIN10REALTEL高清音频管理器如何调音?调音无效怎么办?

    从win7升级到win10,发现realtel高清音频管理器不能调节高级音响效果了,即使设置也没用. 经过研究后发现是驱动 问题,不是驱动 没有安装,而是可能 没有安装完整,于是下载了驱动精灵,重新安 ...

  3. from语法导入

    """# 在执行文件中能不能直接使用模块中的名字 # import m1# import m1 as m# print(m1.a)# print(m1.b)# print ...

  4. [LeetCode] 24. 两两交换链表中的节点

    题目链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是 ...

  5. openwrt 里LUA程序怎么获取POST数据?

    https://www.zhihu.com/question/31579325 作者:齐葛链接:https://www.zhihu.com/question/31579325/answer/28342 ...

  6. Cordova入门系列(三)Cordova插件调用 转发 https://www.cnblogs.com/lishuxue/p/6018416.html

    Cordova入门系列(三)Cordova插件调用   版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心 ...

  7. 随心测试_软测基础_007<软测学习路线建议>

    如果你对软测感兴趣,那么如何学习软件测试呢? 贴心小提示:以下内容,仅供参考,不挖坑 1:学习方式_职业教育选择观 ————SX的观点:成本 | 收益说,理性接受 软测产业服务链中,测试工程师重在于& ...

  8. 02Spring Boot配置文件详解

    02Spring Boot配置文件详解 文章指导 学习笔记 学习代码 自定义属性 在src/main/java/resources目录下创建一个application.properties或appli ...

  9. MediaManager配置公网访问功能

    安装时设置传输本地地址及端口,如图: 路由器设置端口映射,如下图 使用时,打开公网地址http://IpAddress:8090/ContentManager/MainPage.aspx?zh-CN# ...

  10. LODOP获取打印机状态码和状态码含义测试

    由于打印机千差万别,打印机执行的标准也不一样,LODOP获取的打印状态码也可能不同,安装了个打印机驱动实际测试一下,测试的打印机驱动是Brother Color Type3 Class Driver. ...