今天忙中看了下群,群友发了这样一个面试题
let a = 10;
if(function b(){}){
a += typeof (b);
}
console.log(a);
当时看了之后理所当然的认为a为10function
,但万万没想到结果是10undefined
。
然后又给出了这样一个例子:
let m = function test(){};
console.log(test); // test is not defined
然后我也试了一下
const m = function test(){
console.log(test)
}
m()
/* test(){
console.log(test)
} */
console.log(test)
//Uncaught ReferenceError: test is not defined at
发现确实在外面是无法访问该函数的
回想起来以前写一次性事件时其实是用过类似的写法的
document.querySelector("body").addEventListener("click",function handler(){
console.log("click");
// this.removeEventListener("click", arguments.callee); // 严格模式下不能用callee
this.removeEventListener("click",handler);
});
console.log(handler);
// Uncaught ReferenceError: handler is not defined at <anonymous>:1:1
评论