个人整理的答案不一定对 仅供参考
1:让下面的代码可以运行
const a = [1, 2, 3, 4, 5];
// Implement this
a.multiply();
console.log(a); // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]
答
const a = [1, 2, 3, 4, 5];
Object.defineProperty(Array.prototype, 'multiply', {
enumerable: false,
configurable: false,
writable: false,
value: function() {
// let [...result] = this;
let result = this;
this.forEach(item => result.push(item * item));
return result;
}
})
a.multiply()
2: 以下代码运行返回的事false 解释为什么会这样
// false
0.2 + 0.1 === 0.3
答
Number.EPSILON
ES6 在Number对象上面,新增一个极小的常量Number.EPSILON。根据规格,它表示 1 与大于 1 的最小浮点数之间的差。
对于 64 位浮点数来说,大于 1 的最小浮点数相当于二进制的1.00…001,小数点后面有连续 51 个零。这个值减去 1 之后,就等于 2 的 -52 次方。
Number.EPSILON === Math.pow(2, -52)
// true
Number.EPSILON
// 2.220446049250313e-16
Number.EPSILON.toFixed(20)
// "0.00000000000000022204"
Number.EPSILON实际上是 JavaScript 能够表示的最小精度。误差如果小于这个值,就可以认为已经没有意义了,即不存在误差了。
引入一个这么小的量的目的,在于为浮点数计算,设置一个误差范围。我们知道浮点数计算是不精确的。
0.1 + 0.2
// 0.30000000000000004
0.1 + 0.2 - 0.3
// 5.551115123125783e-17
5.551115123125783e-17.toFixed(20)
// '0.00000000000000005551'
上面代码解释了,为什么比较0.1 + 0.2与0.3得到的结果是false。
3:javaScript 中有哪些不同的数据类型
答
1: 分类基本数据类型和引用数据类型
2:基本数据类型 Number String Boolean Undefined null symbol
3: 引用数据类型 Object (包括 Object Array Function)
4: 解决一下异步代码的问题
获取并计算属于某个班级(假设 ID 为 75)的每个学生的平均分数。每个学生在一年内可以参加一门或多门课程。以下 API 可用于获取所需的数据。
// GET LIST OF ALL THE STUDENTS
GET /api/students
Response:
[{
"id": 1,
"name": "John",
"classroomId": 75
}]
// GET COURSES FOR GIVEN A STUDENT
GET /api/courses?filter=studentId eq 1
Response:
[{
"id": "history",
"studentId": 1
}, {
"id": "algebra",
"studentId": 1
},]
// GET EVALUATION FOR EACH COURSE
GET /api/evaluation/history?filter=studentId eq 1
Response:
{
"id": 200,
"score": 50,
"totalScore": 100
}
编写一个以班级 ID 作为参数的函数,你将使用这个函数计算该班级中每个学生的平均分数。这个函数的最终输出应该是带有平均分数的学生列表:
[
{ "id": 1, "name": "John", "average": 70.5 },
{ "id": 3, "name": "Lois", "average": 67 },
}
答
方案一
回调函数(Callback)
function firstAjax() {
ajax(url1, () => {
// 处理逻辑
secondAjax()
})
}
function secondAjax() {
ajax(url2, () => {
// 处理逻辑
})
}
ajax(url, () => {
// 处理逻辑
firstAjax()
})
问题1: 嵌套函数存在耦合性,一旦有所改动,就会牵一发而动全身
问题2: 嵌套函数一多,就很难处理错误
方案2:
Generator
function *fetch() {
yield ajax(url, () => {})
yield ajax(url1, () => {})
yield ajax(url2, () => {})
}
let it = fetch()
let result1 = it.next()
let result2 = it.next()
let result3 = it.next()
function *foo(x) {
let y = 2 * (yield (x + 1))
let z = yield (y / 3)
return (x + y + z)
}
let it = foo(5)
console.log(it.next()) // => {value: 6, done: false}
console.log(it.next(12)) // => {value: 8, done: false}
console.log(it.next(13)) // => {value: 42, done: true}
Promise
方案3:
ajax(url)
.then(res => {
console.log(res)
return ajax(url1)
}).then(res => {
console.log(res)
return ajax(url2)
}).then(res => console.log(res))
async 及 await
方案4
async function test() {
// 以下代码没有依赖性的话,完全可以使用 Promise.all 的方式
// 如果有依赖性的话,其实就是解决回调地狱的例子了
await fetch(url)
await fetch(url1)
await fetch(url2)
}
4: 节流和防抖动的概念
// 节流 触发条件 scroll、touchmove 按照设定的时间固定时间执行一次
function Throttle(fn, delay) {
let timer = null;
var delay = delay || 200;
return function() {
if (timer) return;
timer = setTimeout(function() {
fn.apply(this, arguments);
timer = null;
}, delay)
}
}
// 抖动停止后的时间超过设定的时间 执行一次
function Debounce(fn, delay = 1000) {
timer = null;
return function () {
if (timer) {clearTimeout(timer)}
timer = setTimeout(() => {
fn.apply(this,arguments);
timer = null
}, delay);
}
}
大搜车:
1: 请问一下那个属性值可以让css盒模型中padding的值被包含到元素中宽高
border-box
2: 用一行代码将数组[1,2,3,4] 随机打乱
arr.sort(() => Math.random() - 0.5);
3: 下面那个http response header 不回影响浏览器缓存行为 C
a: cache-control b:etag c:age d: last-modified
4:挑选一个公司或者个人项目,简单描述在项目中遇到的技术问题以及解决思路
5: 自己构思一个无限嵌套的tree 插件 支持尽量多个使用场景(自己设计) 描述组件的调用参数 回调 事件等 (把自己定位一个组件的开发者 开始组件开发时 设计一个给使用者的文档 描述如何调用组件 不需要提及如何实现)
6: 描述vue或者react 的生命周期 并简要描述每个生命周期都做了什么事情
7: 实现一个div的横向居中
8: 字符串查找
// 请使用最基本的遍历实现判断字符串a 是否被包含在字符串b中 ,并返回第一次出现的位置
1: 找不到返回-1
2: 算法效率尽量要高
3: 不要使用 indexOf 正则 substr substring contain slice 等现成的语法
// a=‘34’ b=‘123456’ 返回2
// a=‘35’ b=‘123456’ 返回-1
// a=‘355’ b=‘123543556’ 返回5
9:函数操作
实现这样一个函数 multiply 可以被这样调用 实现无限多数的乘法
multiply(1)(2)(3) //6
multiply(1)(2)(3)()...(n)() //(1)*(2)*(3)*(4)*(...)*(n)()
闪电降价
1:js中阻止事件冒泡方法及其区别
2:解释说明jsonp的工作原理
3:一个URL用js获取参数的值,写出实例
4:call 和apply的区别
5:jQuery 插件的写法 写一个验证表单输入的合法性插件 ,要求输入英文单词开头 数字结尾(要求,用正则表达式匹配)
6: var test = [[“北京”,“上海”,“深圳”],[“南京”,“无锡”,“苏州”],[“杭州”,“金华”,“上海”]] 把数组去重复