箭头函数是匿名函数,不能做为构造函数,不能使用newjavascript
箭头函数不绑定arguments,取而代之用rest参数 (...rest)
前端
箭头函数不绑定this,会捕获其所在的上下文的this值,做为本身的this值.其中须要注意的是: 箭头函数的 this 永远指向其上下文的 this ,任何方法都改变不了其指向,如 call() , bind() , apply()java
var obj = {
a: 10,
b: () => {
console.log(this.a); // undefined
console.log(this); // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
},
c: function() {
console.log(this.a); // 10
console.log(this); // {a: 10, b: ƒ, c: ƒ}
}
}
obj.b(); // window
obj.c(); // 10
复制代码
var a = ()=>{
return 1;
}
function b(){
return 2;
}
console.log(a.prototype); // undefined
console.log(b.prototype); // {constructor: ƒ}
复制代码
if(true) {
let a = 'name'
}
console.log('a',a) // a is not defined
复制代码
变量提高指的是变量声明的提高,不会提高变量的初始化和赋值。react
function fn() {
console.log('a', a);
var a = 1;
function a () {
console.log('I am a function');
}
}
fn() // ƒ a () {console.log('I am a function');}
复制代码
使用 let 的时候面试
function fn1() {
console.log('a', a);
let a = 1;
function a () {
console.log('I am a function');
}
}
fn1();
VM1868:4 Uncaught SyntaxError: Identifier 'a' has already been declared
复制代码
三、使用 var 能够重复声明变量,可是 ==let 不容许在同一块做用域内重复声明同一个变量==, 和 const 不能重复命名segmentfault
四、==let 不容许设置全局变量==,var 能够跨域
不能修改值,const 声明的变量必须通过初始化。缓存
以上 let 的规则一样适用于 const,可是跟 let 的区别是 const 声明的变量不能从新赋值,因此 const 声明的变量必须通过初始化。app