const str='gloabl'
const obj={
str:'private',
getStr:function(){
console.log(this.str)
}
}
obj.getStr()//private 调用者是obj
const s=obj.getStr
s() //gloabal/uundefined 无调用者-window 严格模式为undefined 非严格模式为global
const str='gloabl'
const obj={
str:'private',
getStr:()=>{
console.log(this.str)
}
}
obj.getStr() // global/undefined。 箭头函数不绑定this 它会捕获其所在上下文的this的值,作为自己this的值
箭头函数的特点
箭头函数不能当作构造函数,不能使用new 命令。 箭头函数中this指向固定化,本身没有自己的this ,所以不能用作构造函数
const Fn=()=>{
console.log('...')
}
const f=new Fn()
//Uncaught TypeError: Fn is not a constructor at <anonymous>:4:9
箭头函数没有原型对象
const fn=()=>{
console.log('..')
}
fn.prototype //undefined
const fn2=function(){
console.log('..')
}
fn2.prototype //{constructor: ƒ}
不可以使用arguments对象。 该对象在函数体内不存在。reset替代
const fn=(a,b,c)=>{
console.log(arguments)
}
fn(1,2,3,4,5,6) //arguments is not defined
const fn2=function(a,b,c){
console.log(arguments)
}
fn2(1,2,3,4,5,6) //Arguments(6) [1, 2, 3, 4, 5, 6, callee: ƒ, Symbol(Symbol.iterator): ƒ]
this指向 箭头函数不绑定this 它会捕获其所在上下文的this的值,作为自己this的值