ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-05 11:58:01  阅读:114  来源: 互联网

标签:function 全局变量 console undefined var 编译 test 默认值 log


(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

标签:function,全局变量,console,undefined,var,编译,test,默认值,log
来源: https://blog.csdn.net/qq_43714814/article/details/121727407

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有