JavaScript基础5.参数默认值、递归、预编译、暗示全局变量

5.1参数默认值

参数没有传值,默认值:undefined

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

设置函数默认值的两种方式

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

谁不是undefined,选谁(映射关系)

function test(a = 1,b ){
  console.log(a);
  console.log(b);
}
test(undefined ,2);//1 2
function test(a = undefined,b ){
  console.log(a);
  console.log(b);
}
test(1 ,2);//1 2
  1. 利用arguments赋
function test(a, b){
  var a = arguments[0] || 1;
  var b = arguments[1] || 2;
  console.log(a + b);
}
test();//3
function test(a, b){
  var a = arguments[0] || 1;
  var b = arguments[1] || 2;
  console.log(a + b);
}
test(3,4);//7
function test(a, b){
  var a, b;
  if(typeof(arguments[0]) !== 'undefined'){
    a = arguments[0];;
  }else{
    a = 1;
  }
   if(typeof(arguments[1]) !== 'undefined'){
     b = arguments[1];
  }else{
    b = 2;
  }
}
test(3,4);//7
function test(a, b){
  var a = typeof(arguments[0]) !== 'undefined' ? arguments[0] : 1;
  var b = typeof(arguments[1]) !== 'undefined' ? arguments[0] : 2;
}
test(3,4);//7

5.2递归

  1. 定义一个函数,从WP接收一个n,算出n的阶乘,不能用for
function fact(n){
  if(n == 1){
    return 1;
  }
  return n * fact(n-1);
}
var num = parseInt(window.prompt('算出n的阶乘'));
console.log(fact(num));
  1. 定义一个函数,从WP接收一个n, 算出斐波那契的第n位,不能用for循环
function fb(n){
  if(n <= 0){
    return 0;
  }
  if(n <= 2){
    return 1;
  }
  return fb(n-1) + fb(n-2);
}
var num = parseInt(window.prompt('算出斐波那契的第n位'));
console.log(fb(num));

5.3预编译

JS引擎怎么工作

  1. 检查通篇的语法错误
  2. 预编译的过程
  3. 解释一行,执行一行

函数声明整体提升

test();//可以执行
function test(){
  console.log(1);
}

变量只有声明提升,赋值不能提升

console.log(a);//undefined
var a = 10;
console.log(a);//undefined
var a ;
console.log(a);
function a(a){
  var a = 10;
  var a = function(){
    
  }
}
var a = 1;

image.png

5.4暗示全局变量 imply global variable

全局写不写var,都属于window

a = 1;
console.log(a);//1
var a = 1;
console.log(a);//1
var a = 1;
b =2;
//a = window.a
//b = window.b

在函数内部,没有声明这个变量,直接给变量赋值,提升变量

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

AO activation object 活跃对象,函数上下文
AO = {
a : undefined ->2 -> function a(){}->1
b : undefined ->function(){}
d : function d(){}
}
步骤

   1. 寻找形参和变量声明
   2. 实参值赋值给形参
   3. 找函数声明和赋值函数体
   4. 执行
function test(a, b){
  console.log(a);//1
  c = 0;
  var c;
  a = 5;
  b = 6;
  console.log(b);//6
  function b(){}
  function d(){}
  
  console.log(b);//6
}
test(1);

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

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

GO global object 全局上下文
GO = {
a : undefined- >function a(){}->1
}
步骤

  1. 找变量
  2. 找函数声明
  3. 执行

GO === window

console.log(a, b);//function a(){}  undefined
function a(){} 
var b = function(){}

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

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

GO ={
b :1
}
AO ={
a :undefined->1
}

var b = 3;
console.log(a);// function a(a){...}
function a(a){
  console.log(a);// function a(){...}
  var a = 2;
  console.log(a);//2
  function a(){
    var b = 5;
    console.log(b);//5
  }
}
a(1);

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

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

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

function test(){
  console.log(b);//undefined
  if(a){
    var b = 2;
  }
  c = 3;
  console.log(c);//3
}
var a;
test();
a = 1;
console.log(a);//1

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

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

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

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

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

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

GO = {
a : undefined-> test : function test(e){…}
f : undefined ->5
}
AO ={
e : 1-> function e(){}->2
b : undefined
c : undefined
a : undefined-> 4
}

面试题

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('没通过');
}
if(1 + 5 * '3' === 16){
  console.log('通过了');//执行
}else(){
  console.log('没通过');
}
console.log(!!' ' + !!'' - !!false || '未通过');//1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值