|
阅读:5679回复:0
js 面试题分享 - 作用域、this、执行顺序等灵活使用
请写去下面程序片段对应的输出结果
题1: (function () {
try{
throw new Error();
}
catch (x) {
var x=1,y=2;
console.log(x);
}
console.log(x);
console.log(y);
})();题 2:var test =(function (a) {
this.a =a;
return function (b) {
return this.a + b;
}
}(function (a,b) {
return a;
}(1,2)));
console.log(test(4));题3:var x = 12,y=4;
var obj={
x:1,
y:6,
getX:function () {
var x =5;
return function () {
return this.x;
}();
},
getY:function () {
var y=7;
return this.y;
}
}
console.log(obj.getX());
console.log(obj.getY());答案:题1 结果为:1, undefined,2 //考虑局部变量作用域提升及catch块作用域 题2结果为:5 ; // 主要考虑程序的执行顺序,只是代码相对复杂点 题3结果为:12,6 ;// 主要考查this的指向 你理解了嘛??,有任意问题,欢迎提问^_^ |
|
|