// 变量提升,函数在变量前面 functiona(){ alert(10) } var a // 函数和变量同名,如果变量没有被赋值则函数生效,否则变量生效 alert(a); // 执行函数 a(); // 给a赋值,a为值3 a = 3; functiona(){ alert(10) } // 弹出a的值3 alert(a) // 给a赋值为6 a = 6 // 报错,a不是函数,a值为6 a()
函数表达式
1 2 3 4 5 6 7 8 9 10
var a = functionaaa(num){ aaa = num; console.log(typeof aaa); console.log(aaa); return1; } // 执行a(1),进入函数内部,内部打印为aaa类型function,因为aaa为只读,内容为函数体 a(1) // undefined,aaa函数仅供a内部调用 console.log(typeof aaa);
this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 谁调用this,this就指向谁,没人调用指window,箭头函数的this绑定父元素 this.test = 11; var s = { a:function(){ var test = 2 console.log(1+this.test) console.log(this.test) }, qq:2 } var f = s.a.bind(this); // new之后,this指定当前函数实例 new f(); // NAN undefined
简写的函数声明
1 2 3 4 5 6 7 8 9 10 11
// 简写的函数声明b(){}不能被实例化new this.test = 11; var s = { a(){ console.log(1+this.test) }, qq:2 } var f = s.a.bind(this); new f() // 报错提示f is not a constructor
TDZ暂时性死区
代码块内,使用let、const命令声明变量之前,该变量都是不可用的
1 2 3 4 5 6 7
var a = 11 functionfn(){ console.log(a) let a = 3 } fn() // ReferenceError: Cannot access 'a' before initialization