关于14道魔鬼js考题的整理

1.(function(){

  return typeof arguments

})();

这里返回时是argument类型,它是个类数组,也就对象,所以是object,准确谁是[object argumens]

2

var f = function g(){

return 23;

};

typeof g();

本题是命名函数表达式,但是函数名g只在命名作用域内有效,所以结果是错误,注意本题在老的ie下是解析为函数声明,也就是结果是number;

3

(function(x){

delete x;

return x;

})(1);

本题考察delete删除对象属性;所以此处返回为1;

4

var y = 1, x = y = typeof x;

x;

很明显,undefined

5

(function f(f){

return typeof f();

})(function(){ return 1; });

number

6

var foo = {

bar: function() {

return this.baz;

},

baz: 1

};

(function(){

return typeof arguments[0]();

})(foo.bar);

答案:undefined;

7

var foo = {

bar: function(){

return this.baz;},

baz: 1}

typeof (f = foo.bar)();

本题阵亡了,答案undefined,注意对象只持有方法的引用,并不持有方法

8

var f = (

 function f(){

return "1"; 

},

 function g(){

return 2;

}

)();

typeof f;

答案是number;

9

var x = 1;
if (function f(){}) {
  x += typeof f;
}
x;

1undefined
10
var x = [typeof x, typeof y][1];
typeof typeof x;
x是undefined
x的type是string;

11
(function(foo){
  return typeof foo.bar;
})({ foo: { bar: 1 } });
很明显undefined;
12
(function f(){
  function f(){ return 1; }
  return f();
  function f(){ return 2; }
})();
答案是2,变量提升原则
13
function f(){ return f; }
new f() instanceof f;
false
14
with (function(x, undefined){}) length;
返回时他arguments的length










原文地址:https://www.cnblogs.com/lyz1991/p/5350976.html