原文宣布于我的 github
第23题
[1 < 2 < 3, 3 < 2 < 1]
这个题也还能够.
这个题会让人误以为是 2 > 1 && 2 < 3
实在不是的.
这个题等价于
1 < 2 => true;
true < 3 => 1 < 3 => true;
3 < 2 => false;
false < 1 => 0 < 1 => true;
答案是 [true, true]
第24题
// the most classic wtf
2 == [[[2]]]
这个题我是猜的. 我猜的 true
, 至于为何…..
both objects get converted to strings and in both cases the resulting string is "2"
我不能佩服…
第25题
3.toString()
3..toString()
3...toString()
这个题也挺逗, 我做对了 :) 答案是 error, '3', error
你假如换一个写法就更费解了
var a = 3;
a.toString()
这个答案就是 '3'
;
为啥呢?
因为在 js 中 1.1
, 1.
, .1
都是正当的数字. 那末在剖析 3.toString
的时刻这个 .
究竟是属于这个数字照样函数挪用呢? 只能是数字, 因为3.
正当啊!
第26题
(function(){
var x = y = 1;
})();
console.log(y);
console.log(x);
答案是 1, error
y 被赋值到全局. x 是局部变量. 所以打印 x 的时刻会报 ReferenceError
第27题
var a = /123/,
b = /123/;
a == b
a === b
纵然正则的字面量一致, 他们也不相称.
答案 false, false
第28题
var a = [1, 2, 3],
b = [1, 2, 3],
c = [1, 2, 4]
a == b
a === b
a > c
a < c
字面量相称的数组也不相称.
数组在比较大小的时刻根据字典序比较
答案 false, false, false, true
第29题
var a = {}, b = Object.prototype;
[a.prototype === b, Object.getPrototypeOf(a) === b]
知识点:
只要 Function 具有一个 prototype 的属性. 所以 a.prototype
为 undefined
.
而 Object.getPrototypeOf(obj)
返回一个详细对象的原型(该对象的内部[[prototype]]
值)
答案 false, true
第30题
function f() {}
var a = f.prototype, b = Object.getPrototypeOf(f);
a === b
f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy.
f.prototype 是运用运用 new 建立的 f 实例的原型. 而 Object.getPrototypeOf 是 f 函数的原型.
请看:
a === Object.getPrototypeOf(new f()) // true
b === Function.prototype // true
答案 false
31
function foo() { }
var oldName = foo.name;
foo.name = "bar";
[oldName, foo.name]
答案 ['foo', 'foo']
知识点:
因为函数的名字不可变.
第32题
"1 2 3".replace(/\d/g, parseInt)
知识点:
str.replace(regexp|substr, newSubStr|function)
假如replace函数传入的第二个参数是函数, 那末这个函数将接收以下参数
match 首先是婚配的字符串
p1, p2 …. 然后是正则的分组
offset match 婚配的index
string 悉数字符串
因为问题中的正则没有分组, 所以等价于问
parseInt('1', 0)
parseInt('2', 2)
parseInt('3', 4)
答案: 1, NaN, 3
第33题
function f() {}
var parent = Object.getPrototypeOf(f);
f.name // ?
parent.name // ?
typeof eval(f.name) // ?
typeof eval(parent.name) // ?
先说以下答案 'f', 'Empty', 'function', error
这个答案并不主要…..
这里第一小问和第三小问很简朴不诠释了.
第二小问笔者在本身的浏览器测试的时刻是 ''
, 第四问是 'undefined'
所以应该是平台相干的. 这里邃晓 parent === Function.prototype
就好了.
第34题
var lowerCaseOnly = /^[a-z]+$/;
[lowerCaseOnly.test(null), lowerCaseOnly.test()]
知识点:
这里 test 函数会将参数转为字符串. 'nul'
, 'undefined'
天然都是全小写了
答案: true, true
第35题
[,,,].join(", ")
[,,,] => [undefined × 3]
因为javascript 在定义数组的时刻许可末了一个元素后跟一个,
, 所以这是个长度为三的希罕数组(这是长度为三, 并没有 0, 1, 2三个属性哦)
答案: ", , "
第36题
var a = {class: "Animal", name: 'Fido'};
a.class
这个题比较地痞.. 因为是浏览器相干, class
是个保留字(如今是个关键字了)
所以答案不主要, 主要的是本身在取属性称号的时刻只管防止保留字. 假如运用的话请加引号 a['class']
第37题
var a = new Date("epoch")
知识点:
简朴来讲, 假如挪用 Date 的组织函数传入一个字符串的话须要相符范例, 即满足 Date.parse 的条件.
别的须要注重的是 假如花样毛病 组织函数返回的还是一个Date 的实例 Invalid Date
.
答案 Invalid Date
第38题
var a = Function.length,
b = new Function().length
a === b
我们晓得一个function(Function 的实例)的 length
属性就是函数署名的参数个数, 所以 b.length == 0.
别的 Function.length 定义为1……
所以不相称…….答案 false
第39题
var a = Date(0);
var b = new Date(0);
var c = new Date();
[a === b, b === c, a === c]
照样关于Date 的题, 须要注重的是
假如不传参数等价于当前时候.
假如是函数挪用 返回一个字符串.
答案 false, false, false
第40题
var min = Math.min(), max = Math.max()
min < max
知识点:
风趣的是, Math.min 不传参数返回 Infinity
, Math.max 不传参数返回 -Infinity
?
答案: false
第41题
function captureOne(re, str) {
var match = re.exec(str);
return match && match[1];
}
var numRe = /num=(\d+)/ig,
wordRe = /word=(\w+)/i,
a1 = captureOne(numRe, "num=1"),
a2 = captureOne(wordRe, "word=1"),
a3 = captureOne(numRe, "NUM=2"),
a4 = captureOne(wordRe, "WORD=2");
[a1 === a2, a3 === a4]
知识点:
浅显的讲
因为第一个正则有一个 g 选项 它会‘影象’他所婚配的内容, 等婚配后他会从上次婚配的索引继承, 而第二个正则不会
举个例子
var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
// Found abb. Next match starts at 3
// Found ab. Next match starts at 9
所以 a1 = ‘1’; a2 = ‘1’; a3 = null; a4 = ‘2’
答案 [true, false]
第42题
var a = new Date("2014-03-19"),
b = new Date(2014, 03, 19);
[a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]
这个….
JavaScript inherits 40 years old design from C: days are 1-indexed in C’s struct tm, but months are 0 indexed. In addition to that, getDay returns the 0-indexed day of the week, to get the 1-indexed day of the month you have to use getDate, which doesn’t return a Date object.
a.getDay()
3
b.getDay()
6
a.getMonth()
2
b.getMonth()
3
都是套路!
第43题
if ('http://giftwrapped.com/picture.jpg'.match('.gif')) {
'a gif file'
} else {
'not a gif file'
}
知识点:
String.prototype.match 接收一个正则, 假如不是, 根据 new RegExp(obj)
转化. 所以 .
并不会转义
那末 /gif
就婚配了 /.gif/
答案: 'a gif file'
第44题
function foo(a) {
var a;
return a;
}
function bar(a) {
var a = 'bye';
return a;
}
[foo('hello'), bar('hello')]
在两个函数里, a作为参数实在已声清楚明了, 所以 var a; var a = 'bye'
实在就是 a; a ='bye'
所以答案 'hello', 'bye'
悉数完毕!
==================
总结
因为笔者程度有限, 假如诠释有误, 还望指出 ?
经由过程整顿, 笔者发明绝大部分问题都是因为本身关于基本知识或者说某个 API 的参数明白误差才做错的.
笔者的重灾区在原型那一块, 所以此次被虐和整顿照样很有意义呀.
笔者置信 坚固的基本是深切编程的条件. 所以基本书照样要常看啊 ?
末了这些变态题如今看看还变态嘛?