面试题(笔试题)
中科天翔面试题:
一、选择题
1、以下判断结果为 true 的有:(B、D)
A:1="1"
B:1=="1"
c:1==="1"
D:0==false
E:0.1+0.2===0.3
2、以下不属于 JavaScript 基本数据类型的是:(C)
A:Boolean
B:Symbol
C: Array
D: undefined
3、下面哪个选项可以产生 0<=num<=10 的随机整数:(D)
A: Math.floor(Math.random()*6) // 0
B: Math.floor(Math.random()*10) // 4
C: Math. floor(Math. random()*11) // 7
D: Math.ceil(Math.random()*10) // 10
4、下面分别使用JSON.stringify 方法,返回值 res 分别是:(C)
const fn= function(){}
const res = JSON.stringify(fn) // undefined
const num = 123
const res = JSON. stringify(num) // '123'
const res = JSON.stringify(NaN) // null
const b= true
const res = JSON. stringify(b) // 'true'
A:'function'、'123'、'NaN'、'true'
B: undefined、'123'、undefined、'true'
C: undefined、'123'、'null'、'true’
D:undefined、'123'、'null’、undefined
5、以下哪项可以去除变量 str 中的所有空格:(A)
A:str.replace(/\s*/g,"")
B:str.replace(/^\s|\s$/g,"")
C: str.replace(/^\s*/,"" )
D:str.replace(/(\s*$)/g,"")
二、问答题
addEventListener 有哪些参数?
事件名称;
事件处理函数;
捕获还是冒泡。
事件处理函数不一定是函数,也可以是个JavaScript具有handleEvent方法的对象,看下例子:
var o = {
handleEvent: event => console.log(event)
}
document.body.addEventListener("keydown", o, false);
第三个参数不一定是 bool 值,也可以是个对象,它提供了更多选项。
once:只执行一次。
passive:承诺此事件监听不会调用 preventDefault,这有助于性能。
useCapture:是否捕获(否则冒泡)。
实际使用,在现代浏览器中,还可以不传第三个参数,建议默认不传第三个参数,因为冒泡是符合正常的人类心智模型的,大部分业务开发者不需要关心捕获过程。除非你是组件或者库的使用者,那就总是需要关心冒泡和捕获了。
三、逻辑题
1、以下函数通过 console 调用后输出的结果是: (0)
function get_bool(num) {
let bool = 0;
setTimeout(function () {
bool = true;
return bool;
}, 0);
let fn = () => {
if (num !== "0") {
return false;
}
};
if (bool) {
num = 1;
}
if (fn()) {
num = 2;
}
return bool || num;
}
console.log(get_bool(0)); // 0
2、以下函数执行后输出的结果是:(2、10、3、5、4、1)
setTimeout(function () {
console.log(1);
}, 0);
new Promise(function (resolve, reject) {
console.log(2);
for (var i = 0; i < 10000; i++) {
if (i === 10) {
console.log(10);
}
i == 9999 && resolve();
}
console.log(3);
}).then(function () {
console.log(4);
});
console.log(5);