["1", "2", "3"].map(parseInt)
A:["1", "2", "3"]javascript
B:[1, 2, 3]html
C:[0, 1, 2]java
D:other git
解释:该题目的答案为:[1, NaN, NaN];即选择D。该题用到了map与parseInt;parseInt() 函数的语法是parseInt(string, radix);
string 必需。要被解析的字符串。
radix可选。表示要解析的数字的基数。该值介于 2 ~ 36
之间。
若是省略该参数或其值为 0,则数字将以 10 为基础来解析。若是它以 “0x” 或 “0X” 开头,将以 16 为基数。
若是该参数小于 2 或者大于 36,则 parseInt()
将返回 NaN
。github
实际上 map里面的callback函数接受的是三个参数 分别为元素 下标和数组 (虽然不少状况只使用第一个参数)正则表达式
回调函数的语法以下所示:function callbackfn(value, index, array1)
可以使用最多三个参数来声明回调函数。chrome
例:
var a=["1", "2", "3", "4","5",6,7,8,9,10,11,12,13,14,15];
a.map(parseInt);
返回结果为:[1, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 9, 11, 13, 15, 17, 19]数组
[typeof null, null instanceof Object]
A: ["object", false]浏览器
B: [null, false]app
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"
null instanceof
任何类型 都是false
,因此选A。
[ [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时,会抛出错误,在火狐下报错为:TypeError: reduce of empty array with no initial value
因此选A
var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
A: Value is Something
B: Value is Nothing
C: NaN
D: other
解答:这题考的javascript中的运算符优先级
,这里'+'运算符的优先级要高于'?'
因此运算符,其实是 'Value is true'?'Something' : 'Nothing',当字符串不为空时,转换为bool为true,因此结果为'Something',选D
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
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的,这也就形成了死循环。
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 is not invoked for the missing elements(缺乏的元素,不会调用过滤器
)。
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。
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 string()
;其实就是new一个实例对象。要匹配的也是object
;因此答案是Do not know!,选择C。
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
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
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
Array.isArray( Array.prototype )
A: true
B: false
C: error
D: other
死知识,MDN传送门,这是MDN官方给的例子...选A
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
[] == []
A: true
B: false
C: error
D: other
这题考的是数组字面量建立数组的原理和==运算符,首先JS中数组的真实类型是Object
这点很明显typeof []
的值为"object
",而==运算符当左右都是对象时,则会比较其是否指向同一个对象。而每次调用字面量建立,都会创造新的对象,也就是会开辟新的内存区域。因此指针的值天然不同,结果为 false,选B
'5' + 3 '5' - 3
A: 53, 2
B: 8, 2
C: error
D: other
又是一道隐式类型转换的题
加法: 加法运算中,若是有一个操做值为字符串类型,则将另外一个操做值转换为字符串,最后链接起来
减法: 若是操做值之一不是数值,则被隐式调用Number()
函数进行转换
因此第一行结果为字符串运算,为'53'。第二行结果为2,选A
1 + - + + + - + 1
A: 2
B: 1
C: error
D: other
C语言中的经典...对于这种问题,原理什么的不懂,蒙吧,结果是2。选A
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
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
var a = 111111111111111110000, b = 1111; a + b;
A: 111111111111111111111
B: 111111111111111110000
C: NaN
D: Infinity
又是一道考查JavaScript数字的题,与第七题考察点类似。因为JavaScript实际上只有一种数字形式IEEE 754标准的64位双精度浮点数,其所能表示的整数范围为-2^53~2^53
(包括边界值)。这里的111111111111111110000已经超过了2^53
次方,因此会发生精度丢失的状况。综上选B
var x = [].reverse; x();
A: []
B: undefined
C: error
D: window
这题考查的是函数调用时的this
和Array.prototype.reverse
方法。
首先看Array.prototype.reverse方法,首先举几个栗子:
console.log(Array.prototype.reverse.call("skyinlayer")); //skyinlayer console.log(Array.prototype.reverse.call({})); //Object {} console.log(Array.prototype.reverse.call(123)); //123
这几个栗子能够得出一个结论,Array.prototype.reverse方法的返回值,就是this
Javascript中this有以下几种状况:
全局下this,指向window对象
console.log(this); //输出结果: //Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
函数调用,this指向全局window对象:
function somefun(){ console.log(this); } somefun(); //输出结果: //Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
方法调用,this指向拥有该方法的对象:
var someobj = {}; someobj.fun = function(){ console.log(this); }; console.log(someobj.fun()); //输出结果: //Object {fun: function}
调用构造函数,构造函数内部的this指向新建立对象:
function Con() { console.log(this); } Con.prototype.somefun = function(){}; console.log(new Con()); //输出结果: //Con {somefun: function}
显示肯定this:
function somefun(){ console.log(this); }; somefun.apply("skyinlayer"); somefun.call("skyinlayer"); //输出结果: //String {0: "s", 1: "k", 2: "y", 3: "i", 4: "n", 5: "l", 6: "a", 7: "y", 8: "e", 9: "r", length: 10} //String {0: "s", 1: "k", 2: "y", 3: "i", 4: "n", 5: "l", 6: "a", 7: "y", 8: "e", 9: "r", length: 10}
这里报错:Uncaught TypeError: Array.prototype.reverse called on null or undefined
,选C。
Number.MIN_VALUE > 0
A: false
B: true
C: error
D: other
考查的Number.MIN_VALUE的概念,MDN传送门,关键的几句话
翻译:Number.MIN_VALUE表示的是JavaScript中最小的正数
翻译:MIN_VALUE是接近0的数,而不是最小的数
翻译:MIN_VALUE值约等于5e-324,比起更小的值(大于0),将被转换为0
因此,这里是true,选B
顺带把Number的几个常量拉出来:
Number.MAX_VALUE
:最大的正数Number.MIN_VALUE
:最小的正数Number.NaN
:特殊值,用来表示这不是一个数Number.NEGATIVE_INFINITY
:负无穷大Number.POSITIVE_INFINITY
:正无穷大若是要表示最小的负数和最大的负数,可使用-Number.MAX_VALUE
和-Number.MIN_VALUE
[1 < 2 < 3, 3 < 2 < 1]
A: [true, true]
B: [true, false]
C: error
D: other
运算符的运算顺序和隐式类型转换的题,从MDN上运算符优先级,'<'运算符顺序是从左到右,因此变成了[true < 3, false < 1]
接着进行隐式类型转换,'<'操做符的转换规则(来自$雨$的文章《Javascript类型转换的规则》):
因此,这里首先经过Number()转换为数字而后进行比较,true会转换成1,而false转换成0,就变成了[1 < 3, 0 < 1]
因此结果为A
// the most classic wtf 2 == [[[2]]]
A: true
B: false
C: undefined
D: other
又是隐式类型转换的题(汗)
题目做者的解释是:
both objects get converted to strings and in both cases the resulting string is "2"
也就是说左右两边都被转换成了字符串,而字符串都是"2"
这里首先须要对==右边的数组进行类型转换,根据如下规则(来自justjavac的文章《「译」JavaScript 的怪癖 1:隐式类型转换》):
因此右侧被使用toString()方法转换为"2",而后又经过Number("2")转换为数字2进行比较,结果就是true了,选A
3.toString() 3..toString() 3...toString()
A: "3", error, error
B: "3", "3.0", error
C: error, "3", error
D: other
说实话这题有点常见了,不少人都踩过3.toString()的坑(包括我)...虽然JavaScript会在调用方法时对原始值进行包装,可是这个点是小数点呢、仍是方法调用的点呢,因而乎第一个就是error了,由于JavaScript解释器会将其认为是小数点。
而第二个则很好说通了,第一个点解释为小数点,变成了(3.0).toString(),结果就是"3"了
第三个也是,第一个点为小数点,第二个是方法调用的点,可是后面接的不是一个合法的方法名,因而乎就error了
综上,选C
(function(){ var x = y = 1; })(); console.log(y); console.log(x);
A: 1, 1
B: error, error
C: 1, error
D: other
变量提高和隐式定义全局变量的题,也是一个JavaScript经典的坑...
仍是那句话,在做用域内,变量定义和函数定义会先行提高,因此里面就变成了:
(function(){ var x; y = 1; x = 1; })();
这点会问了,为何不是var x, y;
,这就是坑的地方...这里只会定义第一个变量x,而y则会经过不使用var的方式直接使用,因而乎就隐式定义了一个全局变量y
因此,y是全局做用域下,而x则是在函数内部,结果就为1, error,选C
var a = /123/, b = /123/; a == b a === b
A: true, true
B: true, false
C: false, false
D: other
首先须要明确JavaScript的正则表达式是什么。JavaScript中的正则表达式依旧是对象,使用typeof运算符就能得出结果:
console.log(typeof /123/); //输出结果: //"object"
==运算符左右两边都是对象时,会比较他们是否指向同一个对象,能够理解为C语言中两个指针的值是否同样(指向同一片内存),因此两个结果天然都是false
var a = [1, 2, 3], b = [1, 2, 3], c = [1, 2, 4] a == b a === b a > c a < c
A: false, false, false, true
B: false, false, false, false
C: true, true, false, true
D: other
和上题相似,JavaScript中Array的本质也是对象,因此前两个的结果都是false,
而JavaScript中Array的'>'运算符和'<'运算符的比较方式相似于字符串比较字典序,会从第一个元素开始进行比较,若是同样比较第二个,还同样就比较第三个,如此类推,因此第三个结果为false,第四个为true。
综上所述,结果为false, false, false, true,选A
var a = {}, b = Object.prototype; [a.prototype === b, Object.getPrototypeOf(a) === b]
A: [false, true]
B: [true, true]
C: [false, false]
D: other
原型链的题(总会有的),考查的__proto__和prototype的区别。首先要明确对象和构造函数的关系,对象在建立的时候,其__proto__会指向其构造函数的prototype属性
Object其实是一个构造函数(typeof Object的结果为"function"),使用字面量建立对象和new Object建立对象是同样的,因此a.__proto__也就是Object.prototype,而Object.getPrototypeOf(a)与a.__proto__是同样的,因此第二个结果为true
而实例对象是没有prototype属性的,只有函数才有,因此a.prototype实际上是undefined,第一个结果为false
综上,选A
function f() {} var a = f.prototype, b = Object.getPrototypeOf(f); a === b
A: true
B: false
C: null
D: other
仍是__proto__和prototype的区别,二者不是一个东西,因此选B
function foo() { } var oldName = foo.name; foo.name = "bar"; [oldName, foo.name]
A: error
B: ["", ""]
C: ["foo", "foo"]
D: ["foo", "bar"]
考察了函数的name属性,使用函数定义方式时,会给function对象自己添加一个name属性,保存了函数的名称,很好理解oldName为"foo"。name属性时只读的,不容许修改,因此foo.name = "bar";
以后,foo.name仍是"foo",因此结果为["foo", "foo"],选C
PS:name属性不是一个标准属性,不要去使用,除非你想要坑别人
"1 2 3".replace(/\d/g, parseInt)
A: "1 2 3"
B: "0 1 2"
C: "NaN 2 3"
D: "1 NaN 3"
String.prototype.replace、正则表达式的全局匹配和parseInt(又是parseInt...),能够根据题意看出来题目上漏了一个''
首先须要肯定replace会传给parseInt哪些参数。举个栗子:
"1 2 3".replace(/\d/g, function(){ console.log(arguments); }); //输出结果: //["1", 0, "1 2 3"] //["2", 2, "1 2 3"] //["3", 4, "1 2 3"]
一共三个:
这样就很好理解了,又回到以前parseInt的问题了,结果就是parseInt("1", 10), parseInt("2", 2), parseInt("3", 4)因此结果为"1, NaN, 3",选D
function f() {} var parent = Object.getPrototypeOf(f); f.name // ? parent.name // ? typeof eval(f.name) // ? typeof eval(parent.name) // ?
A: "f", "Empty", "function", "function"
B: "f", undefined, "function", error
C: "f", "Empty", "function", error
D: other
又是Function.name属性的题,和三十二题同样样,f.name值为"f",而eval("f")则会输出f函数,因此结果为"function"
接着看parent,parent实际上就是f.__proto__,须要明确的是JavaScript中的函数也是对象,其也有本身的构造函数Function,因此f.__proto__ === Function.prototype结果是true,而Function.prototype就是一个名为Empty的function
console.log(Function.prototype); console.log(Function.prototype.name); //输出结果: //function Empty() {} //Empty
因此parent.name的值为Empty
若是想直接在全局做用域下调用Empty,显示未定义...由于Empty并不在全局做用域下
综上所述,结果为C
var lowerCaseOnly = /^[a-z]+$/; [lowerCaseOnly.test(null), lowerCaseOnly.test()]
A: [true, false]
B: error
C: [true, true]
D: [false, true]
正则表达式的test方法会自动将参数转换为字符串,原式就变成了[lowerCaseOnly.test("null"), lowerCaseOnly.test("undefined")]
,结果都是真,因此选C
[,,,].join(", ")
A: ", , , "
B: "undefined, undefined, undefined, undefined"
C: ", , "
D: ""
JavaScript中使用字面量建立数组时,若是最末尾有一个逗号',',会背省略,因此实际上这个数组只有三个元素(都是undefined):
console.log([,,,].length); //输出结果: //3
而三个元素,使用join方法,只须要添加两次,因此结果为", , ",选C
var a = {class: "Animal", name: 'Fido'}; a.class
A: "Animal"
B: Object
C: an error
D: other
经典坑中的一个,class是关键字。根据浏览器的不一样,结果不一样:
chrome的结果: "Animal"
Firefox的结果:"Animal"
Opera的结果:"Animal"
IE 8以上也是: "Animal"
IE 8 及如下: 报错