JavaScript选择题解答(1-20)

近来做了个heroku上的JavaScript的测试(题目地点),错了一大堆,觉得js的观点另有许多不是很清楚,这里纪录一下

个人博客文章地点

第一题

What is the result of this expression? (or multiple ones)

["1", "2", "3"].map(parseInt) 

A:[“1”, “2”, “3”]

B:[1, 2, 3]

C:[0, 1, 2]

D:other

解答:这里考的是map、parseInt的用法。map会通报三个参数给其作为参数的函数,为(element, index, array),分别为当前的元素、当前元素在数组中的位置、全部数组:

> ["1", "2", "3"].map(function(){console.log(arguments)}) 
["1", 0, Array[3]]
["2", 1, Array[3]]
["3", 2, Array[3]]

而parseInt只吸收两个参数,为(element, radix),element代表须要被转换为int的字符串,radix代表当前字符串里数字的进制数

所以相当于说,效果数组的元素现实分别为为:

parseInt("1", 0)
parseInt("2", 1)
parseInt("3", 2)

parseInt(“1”, 0)的值为1,MDN上可以看到parseInt函数的radix为0时的行动

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with “0x” or “0X”, radix is 16 (hexadecimal) and the remainder of the string is parsed.

If the input string begins with “0”, radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

所以这里radix值现实为10,所以效果为1

而parseInt(“2”, 1)和parseInt(“3”, 2)则确切没法剖析,会天生NaN

所以答案为[1,NaN,NaN],为D

第二题和第五题

What is the result of this expression? (or multiple ones)

[typeof null, null instanceof Object]

A: [“object”, false]

B: [null, false]

C: [“object”, true]

D: other

考核typeof运算符和instanceof运算符,上MDN上看一下typeof运算符,一些基本范例的效果为:
Undefined “undefined”
Null “object”
Boolean “boolean”
Number “number”
String “string”
Any other object “object”
Array “object”

自从javascript制造出来,typeof null的值就是object了

而null instanceof 任何范例 都是false

所以答案为[“object”, false], 选A

第三题

What is the result of this expression? (or multiple ones)

[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)] ]

A: an error

B: [9, 0]

C: [9, NaN]

D: [9, undefined]

这题考的Math.pow和Array.prototype.reduce

Math.pow(base, exponent)接收两个参数:基数、须要盘算的次方

reduce通报给其作为参数的函数几个值:
* previousValue:上一次盘算的效果
* currentValue:当前元素的值
* index: 当前元素在数组中的位置
* array:全部数组

reduce自身接收两个参数,callback和initialValue,分别是reduce的回调函数和盘算初始值–也就是第一次reduce的callback被挪用时的previousValue的值,默许为0

reduce在数组为空且没有定义initialValue时,会抛出毛病,如chrome下:TypeError: Reduce of empty array with no initial value

所以选A

第四题

What is the result of this expression? (or multiple ones)

var val = 'smtg';
console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');

A: Value is Something

B: Value is Nothing

C: NaN

D: other

这题考的javascript中的运算符优先级,MDN传送门,这里’+’运算符的优先级要高于’?’所以运算符,现实上是 ‘Value is true’?’Something’ : ‘Nothing’,当字符串不为空时,转换为bool为true,所以效果为’Something’,选D

第六题

What is the result of this expression? (or multiple ones)

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: Hello undefined

D: Hello World

这题考的是javascript作用域中的变量提拔,javascript的作用于中运用var定义的变量都邑被提拔到一切代码的最前面,因而乎这段代码就成了:

var name = 'World!';
(function () {
    var name;//如今照样undefined
    if (typeof name === 'undefined') {
        name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();

如许就很好理解了,typeof name === 'undefined'的效果为true,所以末了会输出’Goodbye Jack’,选A

第七题

What is the result of this expression? (or multiple ones)

var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
    count++;
}
console.log(count);

A: 0

B: 100

C: 101

D: other

这题考核javascript中的数字的观点:起首明白一点,javascript和其他言语差别,唯一一种数字,IEEE 754规范的64位浮点数,可以示意的整数局限是-2^53~2^53(包括边境值),所以Math.pow(2, 53)即为javascript中所能示意的最大整数,在最大整数在继续增大就会涌现精度丧失的状况,END + 1 的值现实上是即是END的,这也就造成了死循环,所以选D

第八题

What is the result of this expression? (or multiple ones)

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]

考核Array.prototype.filter要领的运用,MDN上有这么一句it is not invoked for indexes which have been deleted or which have never been assigned values,所以效果为空数组,选C

第九题

What is the result of this expression? (or multiple ones)

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: other

浮点数盘算时的精度丧失题目,其他言语也会涌现…至于效果,横竖我是蒙的…chrome中盘算出来的效果:[0.1, 0.20000000000000007],也就是[true, false],选C

第十题

What is the result of this expression? (or multiple ones)

function showCase(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!');
    }
}
showCase(new String('A'));

A: Case A

B: Case B

C: Do not know!

D: undefined

这题考的是运用new要领建立基本范例,运用new要领建立的基本范例,起首来看个栗子(chrome):

> typeof new String("skyinlayer");
"object"
typeof "skyinlayer";
"string"

如许基本上就可以看到效果了,然则为何呢?MDN上的诠释是,字符串字面量和直接挪用String()要领(不运用new挪用组织函数)的效果是原始字符串。JS自动回转化原始字符串到String对象。所以可以在原始字符串上运用用String对象的要领。而在高低文中,在原始字符串的要领被挪用或许从个中猎取属性时,JS会自动包裹原始字符串然后挪用要领或许猎取属性。

所以呢,JS自身有原始字符串和字符串对象之分,只不过在挪用要领和猎取属性时的时刻会自动转换,但typeof运算符运算时是不会转换的。Number和Boolean一样实用

所以这里效果为Do not know!,选C

第十一题

What is the result of this expression? (or multiple ones)

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!');
    }
}
showCase(String('A'));

A: Case A

B: Case B

C: Do not know!

D: undefined

和上题道理一样,不过这里没有运用new来天生字符串,所以天生的效果就是原始字符串,相当于showCase('A'),所以效果就是A了

第十二题

What is the result of this expression? (or multiple ones)

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]

照样JS的数字相干,不过此次考核的是取模,这题我也是瞎蒙的(坚决跪了)。

前两个基本上没什么疑问,必定是true

’13’在举行盘算前则会举行隐式范例转换(JS最恶心的部份之一),细致拜见$雨$的文章《Javascript范例转换的划定规矩》,这里的划定规矩就是将字符串经由过程Number()要领转换为数字,所以效果为13 % 2 ,也就是true

而JS中负数取模的效果是负数,这里-9%2的效果现实上是-1,所认为false

而Infinity对恣意数取模都是NaN,所以是false

综上,效果为[true, true, true, false, false],也就是C

第十三题

What is the result of this expression? (or multiple ones)

parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)

A: 3, 3, 3

B: 3, 3, NaN

C: 3, NaN, NaN

D: other

照样parseInt的题,考的和第一题相似,第一个值为3没什么好说的。假如涌现的数字不符合背面输入的进制,则为NaN,所以第二个值为NaN。而radix为0时的状况第一题下面有引见,这里也是一样为默许10,所以效果为3,所以答案为3, NaN, 3,选D

第十四题

What is the result of this expression? (or multiple ones)

Array.isArray( Array.prototype )

A: true

B: false

C: error

D: other

死学问,MDN传送门,这是MDN官方给的例子…

第十五题

What is the result of this expression? (or multiple ones)

var a = [0];
if ([0]) { 
  console.log(a == true);
} else { 
  console.log("wut");
}

A: true

B: false

C: “wut”

D: other

一样是一道隐式范例转换的题,不过此次斟酌的是’==’运算符,a自身是一个长度为1的数组,而当数组不为空时,其转换成bool值为true。

而==摆布的转换,会运用假如一个操纵值为布尔值,则在比较之前先将其转换为数值的划定规矩来转换,Number([0]),也就是0,因而变成了0 == true,效果天然是false,所以终究效果为B

第十六题

What is the result of this expression? (or multiple ones)

[] == []

A: true

B: false

C: error

D: other

这题考的是数组字面量建立数组的道理和==运算符,起首JS中数组的实在范例是Object这点很明显typeof []的值为”object”,而==运算符当摆布都是对象时,则会比较其是不是指向同一个对象。而每次挪用字面量建立,都邑制造新的对象,也就是会拓荒新的内存地区。所以指针的值天然不一样,效果为 false,选B

第十七题

What is the result of this expression? (or multiple ones)

'5' + 3  
'5' - 3  

A: 53, 2

B: 8, 2

C: error

D: other

又是一道隐式范例转换的题

加法: 加法运算中,假如有一个操纵值为字符串范例,则将另一个操纵值转换为字符串,末了连接起来

减法: 假如操纵值之一不是数值,则被隐式挪用Number()函数举行转换

所以第一行效果为字符串运算,为’53’。第二行效果为2,选A

第十八题

What is the result of this expression? (or multiple ones)

1 + - + + + - + 1 

A: 2

B: 1

C: error

D: other

C言语中的典范…关于这类题目,道理什么的不懂,蒙吧,效果是2

第十九题

What is the result of this expression? (or multiple ones)

var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return '1'; }); 

A: [2, 1, 1]

B: [“1”, “1”, “1”]

C: [2, “1”, “1”]

D: other

又是考的Array.prototype.map的用法,map在运用的时刻,只要数组中被初始化过元素才会被触发,其他都是undefined,所以效果为[“1”, undefined × 2],选D

第二十题

What is the result of this expression? (or multiple ones)

function sidEffecting(ary) { 
  ary[0] = ary[2];
}
function bar(a,b,c) { 
  c = 10
  sidEffecting(arguments);
  return a + b + c;
}
bar(1,1,1)

A: 3

B: 12

C: error

D: other

这题考的是JS的函数arguments的观点:

在挪用函数时,函数内部的arguments维护着通报到这个函数的参数列表。它看起来是一个数组,但现实上它只是一个有length属性的Object,不从Array.prototype继续。所以没法运用一些Array.prototype的要领。

arguments对象其内部属性以及函数形参建立getter和setter要领,因而改变形参的值会影响到arguments对象的值,反过来也是一样

详细例子可以拜见Javascript隐秘花圃#arguments

所以,这里一切的变动都将见效,a和c的值都为10,a+b+c的值将为21,选D

    原文作者:天镶
    原文地址: https://segmentfault.com/a/1190000000406170
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞