5-参数默认值、暗示全局变量、预编译

(1)、函数参数默认值

初始化参数、如果不设置,参数默认值是undefined

function test(a, b){
	console.log(a);
	console.log(b);
}
test(1);  
// 1
// undefined

不传递实参,给行参设置默认值

es6才支持形参赋值

function test(a = 1, b = 2){
	console.log(a);
	console.log(b);
}
test();
// 1
// 2

在实参里给b赋值,让a保持默认值

function test(a = 1, b){
	console.log(a);
	console.log(b);
}
test(undefined, 2);
// 1
// 2

es5如何给形参赋默认值

function test(a,b){
   var a = arguments[0] || 1;
   var b = arguments[1] || 2;
   console.log(a,b); // 1 2 
}
test();

//下面这种写法也可以
// typeof打印出来的都是字符串
function test(a,b){
   var a = typeof(arguments[0]) !== 'undefined' ? arguments[0] : 1;
   var b = typeof(arguments[1]) !== 'undefined' ? arguments[1] : 2;
   console.log(a,b);  //1 2
  }
 test();

(二)、暗示全局变量

imply global variable

window是一个JavaScript内置的对象

变量未被声明就赋值,该变量就是一个暗示全局变量,它的所有权归window这个全局对象,

一切全局变量都归window所有。

var a = 1;
b = 2; // 暗示全局变量
console.log(window.b);

(三)、预编译:

下面语句会报错,而且一条都不会执行。

javascript引擎会:

1、检查通篇的语法错误,并不会一上来就解释执行。

1.5 预编译的过程

2、解释一行,执行一行

console.log(a);
console.log(a)// 中文分号
console.log(a);

现象1

将函数执行放在函数定义之前,依然可以执行这个函数

test();
function test(){
	console.log(1);
}k,

现象2

不管给a赋不赋值,都打印undefined

console.log(a);  // undefined
var a = 10;
console.log(a);  // undefined
var a;

不声明会报错,也就是说声明非常关键

console.log(a); // 报错

根据以上现象得出结论:

函数声明整体提升,变量只有声明提升,赋值是不提升的

1、函数预编译

函数预编译就是在函数执行之前要进行的步骤。

首先创建一个AO对象 AO activation object 活跃对象也叫函数上下文

(1)、寻找函数的形参和变量声明

(2)、把实参的参数值赋值给形参

(3)、寻找函数声明赋值函数体

(4)、执行函数

function test(a){
	console.log(a);
	var a = 1;
	console.log(a);
	function a(){}
	console.log(a);
	var b = function(){}
	console.log(b);
	function d(){}
}
test(2);

(1)、寻找函数的形参和变量声明

AO = {
	a: undefined,
	b: undefined
}

(2)、把实参的参数值赋值给形参

AO = {
	a: undefined -> 2,
	b: undefined
}

(3)、寻找函数声明赋值函数体

AO = {
	a: undefined -> 2 -> function a(){},
	b: undefined
	d: function d(){}
}

(4)、执行函数

AO = {
	a: undefined -> 2 -> function a(){} -> 1,
	b: undefined -> function(){}
	d: function d(){}
}
打印结果:
f a(){}
1
1
f(){}
举例、
function test(a, b){
   console.log(a);
   c = 0;
   var c;
   a = 5;
   b = 6;
   console.log(b);
   function b(){}
   function d(){}
   console.log(b);
}
test(1);

(1)、寻找形参和变量声明

AO = {
	a: undefined,
	b: undefined,
	c: undefined,
}

(2)、将实参赋值给形参

AO = {
	a: undefined -> 1,
	b: undefined,
	c: undefined,
}

(3)、寻找函数声明并赋值函数体

AO = {
	a: undefined -> 1,
	b: undefined -> function b(){} 
	c: undefined 
	d: undefined -> function d(){}
}

(4)、函数执行

AO = {
	a: undefined -> 1 -> 5
	b: undefined -> function b(){} -> 6
	c: undefined -> 0
	d: undefined -> function d(){}
}
打印结果:
1
6
6
2、GO全局上下文

实际上GO === window

1、寻找变量声明

2、寻找函数声明

3、执行

var a = 1;
function a(){
	console.log(2);
}
console.log(a);

(1)、寻找变量声明

GO = {
	a: undefined
}

(2)、寻找函数声明

GO = {
	a:undefined -> function a(){}
}

(3)、执行

GO = {
	a:undefined -> function a(){} -> 1
}

打印结果:

1
举例
console.log(a, b);
function a(){}
var b = function(){}

(1)、寻找变量

GO = {
	b: undefined
}

(2)、寻找函数声明

GO = {
	b: undefined
	a: function a(){}
}

(3)、执行

GO = {
	b: undefined
	a: function a(){}
}

打印结果

f a(){} undefined
例1、
function test(){
	var a = b = 1;
	console.log(a);
}
test();

执行步骤:

首先创建全局上下文GO

GO = {

}

看到函数,创建函数上下文AO

AO = {
	
}

(1)、寻找形参和变量

AO = {
	a: undefined
}

(2)、将实参赋值给形参,这里无

(3)、寻找函数声明赋值函数体,这里无

(4)、执行函数,b没声明,挂在GO上

GO:{
	 b:1
}

将b的值赋值给a

AO = {
	a: undefined -> 1
}
例2、
var b = 3;
console.log(a);
function a(a){
	console.log(a);
	var a = 2;
	console.log(a);
	function a(){}
	var b = 5;
	console.log(b);
	
}
a(1);
GO = {
  b: undefined -> 3
	a: function a(a){}
}
AO = {
	a: undefined -> 1 -> function a(){} -> 2
	b: undefined -> 5
打印结果:
 function a(a){}
 function a(){}
 2
 5
例3
a = 1;
function test(){
	console.log(a);
	a = 2;
	console.log(a);
	var a = 3;
	console.log(a);
}

test();
var a;

(1)、寻找变量

GO = {
	a: undefined
}

(2)、寻找函数声明

GO = {
  a: undefined
	test: function test(){}
}

(3)、寻找形参和变量

AO = {
 a: undefined
}

(4)、实参赋值给形参,这里无

(5)、寻找函数声明并赋值函数体,这里无

(6)、执行

GO = {
  a: undefined -> 1 
	test: function test(){}
}
AO = {
 a: undefined  ->2 -> 3
}

打印结果:

undefined // 自己有就不去GO里面找,所以不打印1
2
3
例4:
function test(){
	console.log(b);
	if(a){
	 var b = 2;
	}
	c = 3;
	console.log(c);
}

var a;
test();
a = 1;
console.log(a);
GO = {
	a: undefined -> 1
	test: function test(){}
	c: 3
}

AO = {
	b:undefined 
}

打印结果:

undefined
3
1
练1:
function test(){
  return a;
  a = 1;
  function a(){}
  var a = 2; 
}
console.log(test())
AO = {
	a: undefined -> function a(){} 
}

执行结果:

f a(){} 
练2:
 function test(){
     a = 1;
     function a(){}
     var a = 2;
     return a;
  }
  console.log(test());
AO = {
	a:undefined -> f a(){} -> 1 ->2
}

执行结果:

2
练3:
a = 1;
function test(e){
   function e(){}
   arguments[0] = 2;
   console.log(e);
   if(a){
     var b = 3;
   }
   var c;
   a = 4;
   var a;
   console.log(b);
   f = 5;
   console.log(c);
   console.log(a);
}
var a;
test(1);
console.log(a);
console.log(f);
GO = {
	a: undefined -> 1 
	test: function test(e){}
	f: 5
}
AO = {
	e:undefined -> 1 -> function e(){} -> 2
	b:undefined 
	c:undefined	
	a:undefined -> 4
}

打印结果:

2
undefined
undefined
4
1
5

一些练习题:

var a = false + 1;
console.log(a); // 1
var b = false == 1;
console.log(b); // false
if(typeof(a) && (-true) + (+undefined) + ''){
    console.log("通过了");
}else{
    console.log("没通过");
}
// 通过了
// typeof(a) 是 'undefined'
// (-true) 是 -1
// +undefined 是 NaN
// NaN - 1 + '' 是 'NaN'
if(1 + 5 * '3' === 16){
	console.log('通过了');
}else{
	console.log('未通过');
}
// 通过了
console.log(!!' ' + !!'' -!!false || '未通过'); // 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值