设置参数的默认值(当函数调用时,实参传值则使用实参值,若不传值则使用默认值)
方法一function test(a,b) {var a = arguments[0] || 1, //若 a的值为undefined 则 执行后面的 赋值1b = arguments[1] || 2;console.log(a+b);}方法二function test(a,b) {var a,b;if(typeof (arguments[0])!=='undefined'){ //此处需注意 typeof的返回值必定是string类型 所以判断的条件也要加''引号string类型a = arguments[0];}else{a = 1;}if(typeof (arguments[1])!=='undefined'){b = arguments[1];}else{b =2;}console.log(a,b);console.log(a+b);}方法三function test(a,b) {var a = typeof(arguments[0])!=='undefined' ? arguments[0] : 1,b = typeof(arguments[1])!=='undefined' ? arguments[1] : 2;console.log(a+b);}
预编译过程
1.通篇的检查语法错误
1.5 预编译的过程
2.解释一行执行一行
2. 函数执行之前进行的步骤
- 创建 AO对象 -> AO activation object 活跃对象, 函数上下文 =>环境
2. 寻找函数中的形参和变量声明
3. 将实参赋值给形参
4. 寻找函数体内函数声明 赋值函数体
5. 执行(赋值等操作)3. 全局上下文 =>环境
- 创建 GO对象 -> 全局上下文 ===window
2. 寻找变量声明
3. 寻找函数声明
4. 执行(赋值等操作)
例1
function test() {var a = b = 1;console.log(a);}test(); // 1// GO {// b: 1 //执行test()时将 b挂载在全局变量// }// AO{// a: undefined 1 //寻找全局变量b 将b的值赋值给a// }执行 1
例2
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);// GO{// test: function test(a){}// }// AO{// a: undefined =>2 => function a(){}// b: undefined// d: function d(){}// }// 执行 function a(){} (a = 1) => 1 1 (b = function () {}) => function (){}// function a(){} 1 1 function (){}
例3
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);// GO{// test: function test(a,b){}// }// AO{// a: undefined 1 5// b: undefined function b(){} 6;// c: undefined 0// d: function d(){} ;// }// 执行 1 6 6// 1 6 6
例4
var a = 1;function a(){console.log(2);}console.log(a);// GO{// a: undefined => function a(){} =>1// }
例5
console.log(a,b);function a(){}var b = function(){}// go{// a: undefined function a(){}// b: undefined// }
例6
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(){} =>start 2// b: undefined =>start 5// }// * function a(a){} function a(){} 2 5
例7
a = 1;function test() {console.log(a);a = 2;console.log(a);var a = 3;console.log(a);}test();var a;/*Go = {a : undefined =>1test: function test() {}}Ao = {a : undefined => 2 => 3}函数里面找不到 找外面的 a 1 2 3 错误函数里面有 a 只是初始化 undefined 正确的值 undefined 2 3如果连undefined都没有去外面找*/
例8
function test() {console.log(b);if(a){var b = 2;}c = 3;console.log(c);console.log(a);}var a;test();a = 1;console.log(a);/*Go = {a : undefined}Ao = {b : undefinedc : undefined 3}/*undefined 3 undefined 1
例9
function test() {return a;a = 1;function a() {}var a = 2;}console.log(test());/*Go = {test: function test() {}}Ao = {a:undefined function a() {} =>start 2}function a() {}
例10
function test() {a = 1;function a() {}var a = 2;return a;}console.log(test());/*Go = {test: function test(){}}Ao = {a: undefined => 1 function a(){} =>start 2}*/
例11
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 = {test: function test(e){}a: undefined => start 1f: 5}Ao = {e: undefined 1 function e() {} 2a: undefined 4b: undefinedc: undefined}zx 2 undefined undefined 4 1 5
面试题讲解
var a = false + 1;//number(false) = 0+1console.log(a);//1var b = false == 1;console.log(b);//falseif (typeof (a) && (-true) + (+undefined) + "") {console.log('通过了')} else {console.log("没通过")}// // typeof(a) ->"1" -> 非空-> true// // (-true) + (+undefined) + "" =>-1+NaN+""->"NaN" -> 非空 ->trueif (1 + 5 * "3" === 16) {console.log("通过了");} else {console.log("没通过")}console.log(1 + 5 * "3")//16console.log(!!' ' + !!'' - !!false || '为通过')//1console.log(!!' ') //trueconsole.log(!!'') //falseconsole.log(!!false)//faslewindow.a || (window.a = "1");console.log(window.a);//'1'//考察点括号 先执行右边括号里的内容
(4)暗示全局变量
- 暗示全局变量的问题 imply global variable
2. 在全局上的时候 无论是var a = 1;或者a = 1都是放在window对象上的
