JavaScript之深入理解【函数】

function useless(callback){
return callback();
}
var text = 'Johnny';
console.log(useless(function(){ return text; }) == text);//true

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之深入理解【函数】的更多相关文章
- 理解和使用 JavaScript 中的回调函数
理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报 分类: JavaScript(4) 目录( ...
- [转]理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- 【JavaScript】理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- JavaScript callback function 回调函数的理解
来源于:http://mao.li/javascript/javascript-callback-function/ 看到segmentfault上的这个问题 JavaScript 回调函数怎么理解, ...
- 理解javascript中的回调函数(callback)【转】
在JavaScrip中,function是内置的类对象,也就是说它是一种类型的对象,可以和其它String.Array.Number.Object类的对象一样用于内置对象的管理.因为function实 ...
- 深入理解JavaScript执行上下文、函数堆栈、提升的概念
本文内容主要转载自以下两位作者的文章,如有侵权请联系我删除: https://feclub.cn/post/content/ec_ecs_hosting http://blog.csdn.net/hi ...
- 理解与使用Javascript中的回调函数 -2
在javascript中回调函数非常重要,它们几乎无处不在.像其他更加传统的编程语言都有回调函数概念,但是非常奇怪的是,完完整整谈论回调函数的在线教程比较少,倒是有一堆关于call()和apply() ...
- 理解与使用Javascript中的回调函数
在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...
- JavaScript大杂烩2 - 理解JavaScript的函数
JavaScript中的字面量 书接上回,我们已经知道在JavaScript中存在轻量级的string,number,boolean与重量级的String,Number,Boolean,而且也知道了之 ...
- 《JavaScript高级程序设计》读书笔记(三)基本概念第六小节理解函数
内容---语法---数据类型---流程控制语句 上一小节---理解函数 本小节 函数--使用function关键字声明,后跟一组参数以及函数体 function functionName(arg0, ...
随机推荐
- bilibili用户信息全栈爬取
- WEB 性能测试用例设计以及总结
WEB 性能测试用例设计以及总结 WEB 性能测试用例设计模型是设计性能测试用例的一个框架,在实际项目中,需要对其进行适当的剪裁,从而确定性能测试用例的范围和类别.剪裁的依据是性能测试策略和测试范围, ...
- 深入学习:Windows下Git新手教程(上)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangyabin001/article/details/35557231 一,安装Git: 1.1 ...
- Git clone远程目录443:Timed out 问题(go get)
现象: 在cmd中用go get -u github.com/kataras/iris ,提示:443:Timed out 于是在 git bash 中 git clone https://g ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- 使用ES6的模块编写web页面碰到的坑
昨天写最近在做的web应用时,在web页面的js文件中使用了模块功能,这样在html文件中只需要导入一个js就好了
- MySQL实现按天统计数据的方法
一.首先生成一个日期表,执行SQL如下: CREATE TABLE num (i int); ), (), (), (), (), (), (), (), (), (); CREATE TABLE i ...
- 升级:DNAtools for Excel工具箱,2.x英文版- VBA代码破解工具
原始出处:www.cnblogs.com/Charltsing/p/DnaTools.html QQ:564955427 DNA工具箱全部功能一览: 单元格焦点指示(支持Excel 2007~2 ...
- 项目笔记-vue
记录新建vue项目之后的种种. 2019年3月12日,从git上下载了一个vue后台模板的项目,git地址:https://github.com/lin-xin/vue-manage-system ( ...
- Linux(Ubuntu)使用日记------Mongodb的安装与使用
1.安装 Linux下安装mongodb还是比较容易的 直接使用apt-get安装即可,命令如下: sudo apt-get install mongodb 安装完成之后进行检验, “mongo sh ...