看看国外的javascript题目,你能全部做对吗?
叶小钗 的博客最近都在讨论面试题目
正好以前也看过一篇,就借花献佛拿出来分享一下 http://perfectionkills.com/javascript-quiz/
附带了自己的理解,答案有争议的地方欢迎大家指出
题目一
2
3
4
|
( function (){ return typeof arguments; })(); |
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
arguments是对象,伪数组有两件事要注意这里:
参数不是数组,它是一个数组一样的物体,你可以使用方括号和整数索引的元素,但方法通常可在一个如推上不存在参数数组
Array.prototype.slice.call(arguments); 转成数组
当然arguments即使是数组,返回的依然是"object",因为数组也是对象,附加:typeof 对类型的判断
https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Operators/typeof
题目二
2
3
|
var f = function g(){ return 23; }; typeof g(); |
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
因为function g(){ return 23; }是函数表达式,事实上只有事一个名字,不是一个函数声明
函数实际上是绑定到变量f,不是g.
指定的标识符在函数表达式虽然有其用途:堆栈跟踪是清晰而不是充斥着无名的函数,你可以有一个匿名函数递归调用本身不使用argument.callee
附非常详细的帖子函数表达式
http://kangax.github.io/nfe/
题目三
2
3
4
5
|
( function (x){ delete x; return x; })(1); |
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
题目四
2
3
|
var y = 1, x = y = typeof x; x; |
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
题目五
2
3
4
|
( function f(f){ return typeof f(); })( function (){ return 1; }); |
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
题目六
var foo = { bar: function() { return this.baz; }, baz: 1 }; (function(){ return typeof arguments[0](); })(foo.bar);
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目七
var foo = { bar: function(){ return this.baz; }, baz: 1 } typeof (f = foo.bar)();
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目八
var f = (function f(){ return "1"; }, function g(){ return 2; })(); typeof f;
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目九
var x = 1; if (function f(){}) { x += typeof f; } x;
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目十
(function f(){ function f(){ return 1; } return f(); function f(){ return 2; } })();
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目十一
function f(){ return f; } new f() instanceof f;
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目十二
function(foo){ return typeof foo.bar; })({ foo: { bar: 1 } });
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目十三
function(foo){ return typeof foo.bar; })({ foo: { bar: 1 } });
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
题目十四
with (function(x, undefined){}) length;
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
答案
分类: javascript