颠覆认知的『JavaScript』篇——05 参数默认值、递归、预编译、暗示全局变量

JS函数默认参数与预编译

一、默认参数值

1.形参默认值

1. 一般情况下,没有实参,形参默认值为undefind

2. 可以在定义函数的时候设置默认值,不过是ES6的写法

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

test();

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

test(2,1);

2. 设置默认值

1.利用arguments || 运算实现

2.判断typeof(arguments[])是否为undefind,注意typeof()返回的是字符串

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

test(); // 5

function test2(a, b) {
  var a = typeof(arguments[0]) === 'undefined' ? 2 : arguments[0];
  var b = typeof(arguments[1]) === 'undefined' ? 3 : arguments[1];
  console.log(a + b);
}
test2(); // 5

二、递归

找规律

給一个出口

三、预编译

1. 步骤

通篇检查语法错误

预编译过程

解释一行,执行一行

2. 变量提升

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

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

3.  AO active 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);


         }
         test(2);
        // AO = {
        //      a: undefind ->
        //         2 ->
        //         function a(){} ->
        //         1
        //      b: function(){0}
        //  }

4. GO  global object,全局上下文

        1. 找变量

        2. 找函数声明

        3. 执行

        var a = 1;
        function a(){}
        console.log(a);
        GO{
            a: undefind ->
               function a(){}
               1
        }

练习题

/**
 * AO = {
 *  a : undefined -> 2 -> function a() {} -> 1
 *  b : undefined -> function() {}
 *  d : function d() {}
 * }
 */
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 = {
 *  a : undefined -> 1 -> 5
 *  b : undefined -> function b(){} -> 6
 *  c : undefined -> 0
 *  d : function d(){}
 * }
 */
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);

//-----------------------------------------------------------------

/**
 * GO = {
 *  a : undefinded -> function a() {} -> 2
 * }
 */
var a = 2;
function a() {
  console.log(2);
}

console.log(a); // 2

//---------------------------------------------------------------

/**
 * GO = {
 *  b : undefined	-> function() {}
 *  a : function a() {}
 * }
 */
console.log(a, b);	// function a() {}  undefined
function a() {}
var b = function() {};

//--------------------------------------------------------------

/**
 * GO = {
 *  test: function (){...}
 *  b: 1
 * }
 * 
 * AO = {
 *  a : undefined -> 1
 * }
 */

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

test();

//--------------------------------------------------------

/**
 * GO = {
 *  b : undefined -> 3
 *  a : function a(a) {}
 * }
 * 
 * AO = {
 *  a : undefined -> 1 -> function a() {} -> 2
 *  b : undefined -> 5
 * }
 */
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 = {
 *  a : undefined -> 1
 *  test : function test() {}
 * }
 * 
 * AO = {
 *  a : undefined - > 2 -> 3
 * }
 */
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() {}
 *  c : 3
 * }
 * 
 * AO = {
 *  b : undefined
 * }
 */
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中c是没有声明的,所以没有变量提升,
// 在if语句中,预编译也不会看是否有a或者成立,
// 它只会看有没有声明变量

四、暗示全局变量

暗示全局变量 imply globlal variable

在函数内部没有声明的变量是全局变量,它会挂上window的属性

var a = 1;
    b = 2;

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

//---------------------------------------
function test() {
  var a = b = 1;
}

test();
// 访问对象中不存在的属性,会返回undefined
console.log(window.a); // undefined
console.log(window.b); // 1

五、面试题

// Number(false) -> 0 
var a = false + 1;
console.log(a); // 1

//这道题考察隐式类型转化

//----------------------------------

// Number(false) -> 0
var b = false == 1;
console.log(b); // false

//隐式类型

//------------------------------------

/**
 * typeof(a) -> 'undefined'
 * (-true) -> Number(-true) -> -1
 * (+undefined) -> Number(+undefined) -> NaN
 * -1 + NaN + '' -> 'NaN'
 * 'undefined' && 'NaN' -> 'NaN'
 * Boolean('NaN') -> true
 */
if (typeof(a) && (-true) + (+undefined) + '') {
  console.log('通过了'); // 执行
} else {
  console.log('没通过');
}

/* 值得注意的是typeof()类型判断的返回值是字符串,
 * 对于未声明的变量,比如a,typeof(a)会打印字符串类型的undefind,而console.log(a)会报错
 * 
 */
//-----------------------------------------------------------------

/**
 * 5 * '3' -> 5 * Number('3') -> 15
 * 1 + 15 === 16 -> true
 */
if (1 + 5 * '3' === 16) {
  console.log('通过了'); // 执行
} else {
  console.log('未通过');
}

//----------------------------------------------------------------

/**
 * Boolean(' ') -> true
 * Boolean(') -> false
 * Boolean(false) -> false
 * true + false - false || '通过了'
 * Number(true) + Number(false) - Number(false) || '通过了'
 * 1 + 0 - 0 || '通过了'
 * 1 || '通过了'
 * 1
 */
console.log(!!' ' + !!'' - !!false || '通过了'); // 1

六、作业

/**
 * AO = {
 *  a : undefined -> function a() {}
 * }
 */
function test() {
  // 遇到return直接返回
  return a;
  a = 1;
  function a() {}
  var a = 2;
}

console.log(test()); // function a() {}

//--------------------------------------------

/**
 * AO = {
 *  a : undefined -> function a() {} -> 1 -> 2
 * }
 */
function test() {
  a = 1;
  function a() {}
  var a = 2;
  return a;
}

console.log(test()); // 2

//------------------------------------------------

/**
 * GO = {
 *  a : undefined -> 1
 *  test: function test(e) {}
 *  f : 5
 * }
 * 
 * AO = {
 *  e : undefined -> 1 -> function e() {} -> 2
 *  b : undefined
 *  c : undefined
 *  a : undefined -> 4
 * }
 */
a = 1;
function test(e) {
  function e() {}
  arguments[0] = 2;
  console.log(e); // 2
  // 此时变量a的值为undefined,不执行if语句内容
  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);
console.log(a); // 1
console.log(f); // 5
//argument[]是可以改变实参的值的

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值