进击JavaScript核心 --- (2)函数和预解析机制
一、函数
function add(a, b) {
return a + b;
}
fn(); //
function fn() {console.log(1)}
var add = function(a, b) {
return a + b;
};
fn(); // Uncaught TypeError: fn is not a function
var fn = function(){console.log(1)};
var flag = true;
if(flag) {
function fn() {
console.log('flag 为true')
}
} else{
function fn() {
console.log('flag 为false')
}
}
fn();
// chrome, firefox, ie11 输出 flag 为true
// ie10及以下 输出 flag 为false
var flag = true;
var fn; if(flag) {
fn = function() {
console.log('flag 为true');
}
} else{
fn = function() {
console.log('flag 为false');
}
}
fn() //chrome, firefox, ie7-11 均输出 flag 为true
var add = function f(a, b) {
console.log(a + b);
}
add(1,2); //
f(1,2); // Uncaught ReferenceError: f is not defined
var add = function f(a, b) {
console.log(f);
}
console.log(add);
add(3, 5);
// ƒ f(a, b) {
// console.log(f);
// }
var add = new Function('a', 'b', 'return a + b');
public void add(int a, int b) {
System.out.println(a + b);
}
public void add(int a, int b, int c) {
System.out.println(a * b * c);
}
// 调用时,会根据传入参数的不同,而选择不同的方法,例如传入两个参数,就会调用第一个add方法
function add(a, b) {
console.log(a + b);
}
function add(a, b, c) {
c = c || 2;
console.log(a * b * c);
}
add(1, 2); // 4 (直接调用最后一个同名的函数,并没有重载)

function fn() {
console.log('hello')
}
fn()
// hello
既然fn是一个函数指针,指代函数的代码段,那能否直接在代码段后面加一对圆括号呢?
function fn() {
console.log('hello')
}()
// Uncaught SyntaxError: Unexpected token )
var fn = function() {
console.log('hello')
}()
// hello
console.log(fn); // ƒ fn() {console.log('hello');}
function fn() {
console.log('hello');
}
// 在function关键字前面加一个合法的字符,结果就把fn当做一个未定义的变量了
console.log(fn); // Uncaught ReferenceError: fn is not defined
+function fn() {
console.log('hello');
}
+function() {
console.log('hello')
}()
-function() {
console.log('hello')
}()
*function() {
console.log('hello')
}()
/function() {
console.log('hello')
}()
%function() {
console.log('hello')
}()
// hello
// hello
// hello
// hello
// hello
(function() {
console.log('hello');
})();
(function() {
console.log('hello');
}());
// hello
// hello
// 3! = 3*2*1
// 4! = 4*3*2*1 = 4*3! function factorial(num) {
if(num <= 1) {
return 1
}
return num * factorial(num - 1)
} console.log(factorial(5)) //
console.log(factorial(4)) //
function fn() {
console.log(arguments.callee)
}
fn()
// ƒ fn() {
// console.log(arguments.callee)
// }
function factorial(num) {
if(num <= 1) {
return 1
}
return num * arguments.callee(num - 1)
}
console.log(factorial(5)) //
'use strict'
function factorial(num) {
if(num <= 1) {
return 1
}
return num * arguments.callee(num - 1)
}
console.log(factorial(5))
// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
var factorial = function jieCheng(num) {
if(num <= 1) {
return 1
}
return num * jieCheng(num - 1)
};
console.log(factorial(5)) //
var result = factorial;
console.log(result(4)); //
function add(a, b) {
console.log(a + b);
}
function sum1(a, b) {
add.apply(window, [a, b]);
}
function sum2(a, b) {
add.apply(this, arguments)
}
sum1(1, 2); //
sum2(3, 5); //
var color = 'red';
var obj = {
color: 'blue'
}; function getColor() {
console.log(this.color)
} getColor.call(this) // red
getColor.call(obj) // blue
二、预解析机制
var color = 'red';
var size = 31; function fn() {
console.log(color);
var color = 'blue';
var size = 29;
} fn(); // undefined

console.log(fn) // ƒ fn() {}
function fn() {}
var fn = 32
console.log(fn); // undefined
var fn = function() {};
var fn = 32;
console.log(fn) //
console.log(fn); // ƒ fn() {console.log('你好 世界')}
function fn() {console.log('hello world')}
function fn() {console.log('你好 世界')}
预解析练习一:
var fn = 32
function fn() {
alert('eeee')
}
console.log(fn) //
fn() // Uncaught TypeError: fn is not a function
console.log(typeof fn) // number
// 按照上面的预解析规则,预解析第一步时,fn会被赋值为 function fn() {alert('eeee')};第二步从上到下逐步执行时,由于函数fn声明提前,优于var声明的fn执行了,
// 所以fn会被覆盖为一个Number类型的基本数据类型变量,而不是一个函数,其值为32
预解析练习二:
console.log(a); // function a() {console.log(4);}
var a = 1;
console.log(a); //
function a() {
console.log(2);
}
console.log(a); //
var a = 3;
console.log(a); //
function a() {
console.log(4);
}
console.log(a); //
a(); // 报错:不是一个函数
var a = 1;
function fn(a) {
console.log(a); //
a = 2;
console.log(a) //
} fn(999);
console.log(a); //

var a = 1;
function fn() {
console.log(a);
var a = 2;
} fn(); // undefined
console.log(a); //
var a = 1;
function fn() {
console.log(a);
a = 2;
} fn(); //
console.log(a); //
进击JavaScript核心 --- (2)函数和预解析机制的更多相关文章
- 轻松搞定javascript变量(闭包,预解析机制,变量在内存的分配 )
变量: 存储数据的容器 1.声明 var 2.作用域 全局变量. 局部变量. 闭包(相对的全局变量): 3.类型 a.基本类型(undefi ...
- JavaScript变量提升和函数声明预解析
1.首先理解函数作用域 在JavaScript中,变量的定义并不是以代码块作为作用域的,而是以函数作用作用域的.也就是说,如果变量是在某个函数中定义的,那么它在函数以外的地方是不可见的.而如果该变量是 ...
- 从var func=function 和 function func()区别谈Javascript的预解析机制
var func=function 和 function func()在意义上没有任何不同,但其解释优先级不同:后者会先于同一语句级的其他语句. 即: { var k = xx(); function ...
- JavaScript 预解析机制
首先我们来看一段代码: <script> console.log(a); var a = 10; </script> 此时运行结果为 为什么会显示undefined呢?这就 ...
- [妙味JS基础]第六课:作用域、JS预解析机制
知识点总结 浏览器的解析方法 script 全局变量,全局函数 自上而下 函数 由里到外 "JS的解析器": 1)“找一些东西”:var function 参数 var a=未定义 ...
- javascript-初级-day06作用域、JS预解析机制
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- ECMAScript1.3 数组 | 函数 | 作用域 | 预解析
数组array 数组可以存储很多项,有顺序,很多项形成一个集合,就是数组. 数组字面量是:[] 如何获取数组中的数据:索引/下标,数组中的第一项的索引是从0开始的. ['kay', 'andy', 1 ...
- 轻松搞定javascript预解析机制(搞定后,一切有关变态面试题都是浮云~~)
hey,guys!我们一起总结一下JS预解析吧! 首先,我们得搞清楚JS预解析和JS逐行执行的关系.其实它们两并不冲突,一个例子轻松理解它们的关系: 你去酒店吃饭,吃饭前你得看下菜谱,点下菜(JS预解 ...
- JS预解析机制
JS的预解析过程: 1,预解析 2,再逐行解读代码, 实例: ---------------------------- <script> var name="xm& ...
随机推荐
- Win2008 Server配置PHP环境
Win2008 Server配置PHP环境 阅读目录 创建一个网站 配置PHP环境 配置iis的“处理应用程序映射” 在配置PHP环境之前要先配置好IIS. 传送门-> Win2008 Se ...
- 如何编写自己的C语言头文件
一些初学C语言的人,不知道头文件(*.h文件)原来还可以自己写的.只知道调用系统库函数时,要使用#include语句将某些头文件包含进去.其实,头文件跟.C文件一样,是可以自己写的.头文件是一种文本文 ...
- Python学习笔记:os模块和sys模块
os模块 os.path.driname(path):返回当前路径的上一级路径字符串. os.path.basename(path):返回当前路径的目录名(文件夹名)或文件名(全称). os.path ...
- 创建Django项目并将其部署在腾讯云上
这段时间在做scrapy爬虫,对爬出来的数据基于Django做了统计与可视化,本想部署在腾讯云上玩玩,但是因为以前没有经验遇到了一些问题,在这里记录一下: 首先说下Django的创建与配置: 1. 创 ...
- python模块之sys
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sys.maxi ...
- Hard problem CodeForces - 706C
Time limit1000 ms Memory limit262144 kB 题目: Vasiliy is fond of solving different tasks. Today he fou ...
- 天问之Linux内核中的不明白的地方
1. Linux 0.11\linux\kernel\exit.c 文件中, 无论是send_sig()函数还是kill_session()函数中,凡是涉及到发送信号的地方,都是直接 (*p)- ...
- XP系统连接win10家庭版共享的打印机方法
1.高级共享设置.按照win7正常设置."家庭网络"公用网络”“工作网络”之类的注意根据当前配置设置! 2.由于控制面板无法开启Guest账户.需要用任务管理器,运行cmd(管理员 ...
- 光学字符识别OCR-5 文本切割
经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...
- 写iOS SDK注意事项
转载http://www.devtang.com/blog/2015/01/31/write-sdk-tips/