gdutjason / front-end-knowledge

0 stars 0 forks source link

JavaScript值得一看的题库 #1

Open gdutjason opened 1 year ago

gdutjason commented 1 year ago

小伙伴们空闲时间可以做这些题目练练手。​ 1、实现一个sum(1,2,3)(4)(5)(6,7)(8)() 2、实现一个sum(1,2,3)(4)(5)(6,7)(8)()升级版:如何实现加,减,乘,除呢? 3、哪些是宏任务、微任务,以及其执行顺序 4、SuperPerson继承Person 5、substr、substring、slice(待补充) 6、slice、splice(待补充) 7、map、reduce、filter(待补充) 8、前端控制并发请求数量

gdutjason commented 1 year ago

实现一个sum(1,2,3)(4)(5)(6,7)(8)() 返回结果为这些数字的和:36。

这是一道考察求和+闭包+递归的题目。

答案 ```javascript function sum(){ const result = [...arguments].reduce((ret, cur)=>ret+cur) return function(){ if(arguments.length === 0)return result return sum(...[...arguments, result]); } } ```
gdutjason commented 1 year ago

实现一个sum(1,2,3)(4)(5)(6,7)(8)()升级版:如何实现加,减,乘,除呢? 除了考察求和,闭包,递归以外,还考察了柯里化函数。

sum和multiple不用关注顺序。 而minus和divide需要注意顺序,因此在return curried(...[result, ...arguments]);中将result前置了。

答案 ``` function curry(callback){ return function curried (){ const result = callback(arguments) return function(){ if(arguments.length === 0)return result return curried(...[result, ...arguments]); } } } let sum = (args) =>{ return [...args].reduce((acc, cur)=>acc+cur) } let minus = (args) =>{ return [...args].reduce((acc, cur)=>acc-cur) } let multiple = (args) =>{ return [...args].reduce((acc, cur)=>acc*cur) } let divide = (args) =>{ return [...args].reduce((acc, cur)=>acc / cur) } let currySum = curry(sum) let curryMultiple = curry(multiple) let curryDivide = curry(divide) let curryMinus = curry(minus) console.log(currySum(1,2,3)(4)(5)(6,7)(8)()) console.log(curryMultiple(1,2,3)(4)(5)(6,7)(8)()) console.log(curryDivide(1,2,3)(4)(5)(6,7)(8)()) console.log(curryMinus(1,2,3)(4)(5)(6,7)(8)()) ```
gdutjason commented 1 year ago

哪些是宏任务、微任务,以及其执行顺序

async function async1() {
    console.log('async1 start');
    await async2();
    console.log('async1 end');
}
async function async2() {
    console.log('async2');
}
console.log('script start');
setTimeout(function() {
    console.log('setTimeout');
}, 0)
async1();
new Promise(function(resolve) {
  console.log('promise1');
  resolve();
}).then(function() {
    console.log('promise2');
});
console.log('script end');

这道题考察哪些是宏任务、微任务,以及其执行顺序,其中settimeout是宏任务,promise的then和await之后的的语句为微任务

答案 ``` script start async1 start async2 promise1 script end async1 end promise2 setTimeout ```
gdutjason commented 1 year ago

SuperPerson继承Person

写一个类Person,拥有属性age和name,拥有方法say(something)
再写一个类Superman,继承Person,拥有自己的属性power,拥有自己的方法fly(height) ES5方式
答案 ``` function Person(age, name){ this.age = age; this.name = name; } Person.prototype.say = function(something) { // ... } function Superman(age, name, power){ Person.call(this, age, name, power); this.power = power; } Superman.prototype = Object.create(Person.prototype); Superman.prototype.constructor = Superman; Superman.prototype.fly = function(height) { // ... } let superman = new Superman(25, 'GaoKai', 'strong'); // class方式 class Person { constructor(age, name){ this.age = age; this.name = name; } say(something){ // ... console.log("say"); } } class Superman extends Person{ constructor(age, name, power){ super(age, name) this.power = power; } fly(height){ // ... console.log("fly"); } } let superman = new Superman(25, 'GaoKai', 'strong'); ```
gdutjason commented 1 year ago

前端控制并发请求数量

答案 ``` class FetchPool { options = { // 最大同时请求数量 max: 6, }; queues = []; count = 0; constructor(options) { this.options = { ...this.options, ...options }; return (input, init) => { // fetch 返回一个 promise return new Promise((resolve, reject) => { // 将这次请求的 input/init,promise 的 resolve/reject 放入队列,等待处理 this.queues.push({ input, init, resolve, reject, }); // 处理队列 this.runQueues(); }) } } runQueues() { if (this.queues.length === 0) return; if (this.count >= this.options.max) return; this.count++; // 从队列中取出一个任务进行处理 const queue = this.queues.shift(); // 处理请求 fetch(queue.input, queue.init).then((res) => { this.count--; this.runQueues(); return queue.resolve(res); }).catch((err) => { this.count--; this.runQueues(); return queue.reject(err); }) } } ```