·1·
30个你不可能全会做的题目
你答对了几道?答错了别急,看看答案,看看本书。
有趣的题-答案
1, 以下表达式的运行结果是什么?
[\
A、[“1”, “2”, “3”] B、[1, 2, 3] C、[0, 1, 2] D、其它
答案:D。
解释:parseInt需要2个参数(val, radix),map却传递了 3 个参数(element, index, array)。
2, 以下表达式的运行结果是什么?
[typeof null, null instanceof Object]
A、[\B、[null, false] C、[\D、其它
答案:A。
解释:Javascript规范规定,null和Object都是javascript中的数据类型,其中null表示空引用的一个特殊值,所以typeof null -> 'object' ( ECMA-262的规定 ),但是null值又不是以Object为原型创建出来的,所以null instanceof Object -> 'false'。
3, 以下表达式的运行结果是什么?
[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
A、报错 B、[9, 0] C、[9, NaN]
· 2·
D、[9, undefined]
答案:A。
解释:根据规范: 在一个空数组上应用reduce会抛初始化错误的异常 TypeError。
4, 以下表达式的运行结果是什么?
var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
A、Something B、Nothing C、NaN D、其它
答案:D。
解释:运行结果实际会打印 'Something','+' 操作符的优先级实际上比三元操作符要高。
5, 以下表达式的运行结果是什么?
var name = 'World!'; (function () {
if (typeof name === 'undefined') { var name = 'Jack';
console.log('Goodbye ' + name); } else {
console.log('Hello ' + name); } })();
A、Goodbye Jack B、Hello Jack
C、Goodbye undefined D、Hello undefined
答案:A。
解释:var 声明的作用域在整个 function 中, 但并没有初始化。简单说,浏览器的JavaScript引擎首先会扫描所有的变量声明,并把变量声明上升到作用域的顶部,但是又不会改变变量赋值的位置。因此在实际的代码运行中,由于内层函数域的重新初始化干扰,外层域初始化'name'是假象,这就导致了在'name'未没初始化赋值之前,就判断其值,肯定是'undefined'。
6, 以下表达式的运行结果是什么?
var END = Math.pow(2, 53); var START = END - 100; var count = 0;
for (var i = START; i <= END; i++) { count++; }
console.log(count);
·3·
A、0 B、100 C、101 D、其它
答案:D。
解释:这段代码会进入死循环, 2^53 是javascript中最大的数字, 2^53+1 与 2^53 等同, 因此 i 永远也不会比这个数大。
7, 以下表达式的运行结果是什么?
var ary = [0,1,2]; ary[10] = 10;
ary.filter(function(x) { return x === undefined;});
A、[undefined × 7] B、[0, 1, 2, 10] C、[]
D、[undefined]
答案:C。
解释:Array.prototype.filter 不会应用到缺少的元素上。
8, 以下表达式的运行结果是什么?
var two = 0.2 var one = 0.1 var eight = 0.8 var six = 0.6
[two - one == one, eight - six == two]
A、[true, true] B、[false, false] C、[true, false] D、其它
答案:C。
解释:JavaScript 没有精确的数字, 即便它看上去有时侯能正常工作。
9, 以下表达式的运行结果是什么?
function showCase(value) {
· 4·
switch(value) {
case 'A':
console.log('Case A'); break; case 'B':
console.log('Case B'); break;
case undefined:
console.log('undefined'); break; default:
console.log('Do not know!'); } }
showCase(new String('A'));
A、Case A B、Case B
C、Do not know! D、undefined
答案:C。
解释:switch 使用 === 来枚举,并且 new String(x) !== x。
10,
以下表达式的运行结果是什么?
function showCase2(value) { switch(value) { case 'A':
console.log('Case A'); break; case 'B':
console.log('Case B'); break;
case undefined:
console.log('undefined'); break; default:
console.log('Do not know!'); } }
showCase2(String('A'));
A、Case A B、Case B
C、Do not know! D、undefined
答案:C。
解释:String(x) 不会返回一个 object 但会返回一个 string。
·5·
11,
以下表达式的运行结果是什么?
function isOdd(num) { return num % 2 == 1; }
function isEven(num) { return num % 2 == 0; }
function isSane(num) {
return isEven(num) || isOdd(num); }
var values = [7, 4, '13', -9, Infinity]; values.map(isSane);
A、[true, true, true, true, true] B、[true, true, true, true, false] C、[true, true, true, false, false] D、[true, true, false, false, false]
答案:C。
解释:Infinity % 2 返回 NaN, -9 % 2 返回 -1。
12,
以下表达式的运行结果是什么?
parseInt(3, 8) parseInt(3, 2) parseInt(3, 0)
A、3, 3, 3 B、3, 3, NaN C、3, NaN, NaN D、其它
答案:D。
解释:结果为“3,NaN,3“,3 在2进制中不存在, 很显然结果是NaN, 但0呢? parseInt 猜测你的意思是10, 所有返回是3。
13,
以下表达式的运行结果是什么?
Array.isArray( Array.prototype )
A、true B、false