当前位置: 首页 > news >正文

用dw做购票网站模板杭州云优化信息技术有限公司

用dw做购票网站模板,杭州云优化信息技术有限公司,网站ui设计例子,宁波网站优化方法文章目录 前言:防抖节流函数柯里化函数组合instanceof 实现实现new操作符的行为深拷贝继承实现:手写Promise数组中常见函数的实现 前言: 在前端面试中,经常会遇到要求手写的代码的题目,主要是考察我们的编程能力、和对…

文章目录

    • 前言:
    • 防抖
    • 节流
    • 函数柯里化
    • 函数组合
    • instanceof 实现
    • 实现new操作符的行为
    • 深拷贝
    • 继承实现:
    • 手写Promise
    • 数组中常见函数的实现

前言:

在前端面试中,经常会遇到要求手写的代码的题目,主要是考察我们的编程能力、和对JavaScript的理解以及对前端最佳实践的掌握。下面是我整理了一些常见的手写代码题目,您可以看看自己能实现哪些。。

防抖

防抖函数,确保一段时间内多次触发事件只执行一次。

// --- 基础版
function debounce(fn, delay) {let timer = 0;return (e) => {if (timer) {clearTimeout(timer);}timer = setTimeout(() => fn(e), delay);};
}
let deboundceCli = debounce((e) => console.log(e), 500);
document.body.addEventListener("click", function () {deboundceCli("执行了。");
});
// ---立即执行版
function debounceImmediate(fn, delay, immediate) {let timer = null;return (...rest) => {if (timer) {clearTimeout(timer);}// 立即执行if (immediate) {let exec = !timer;timer = setTimeout(() => (timer = null), delay);if (exec) {fn(...rest);}} else {timer = setTimeout(() => fn(...rest), delay);}};
}let deboundceCli = debounceImmediate((e) => console.log(e), 500, true);
document.body.addEventListener("click", function () {deboundceCli("执行了。");
});

节流

节流函数,确保在指定时间内只执行一次。

const throttle = (fn, delay) => {let lastTimer = null;return (e) => {if (Date.now() - lastTimer > delay) {lastTimer = Date.now();fn(e);}};
};
let throttleCli = throttle((e) => console.log(e), 1000);
window.document.addEventListener("scroll", function () {throttleCli("执行了。。 ");
});

函数柯里化

能够接受多个参数,并返回一个新的函数。

// ---柯里化
const curry = (fn) => {return function inner() {let len = arguments.length;if (len === fn.length) {// 执行return fn(...arguments);} else {// 返回一个函数return (...res) => inner(...[...arguments, ...res]);}};
};const f1 = (a, b, c) => a * b * c;let cy = curry(f1);
console.log(cy(2)(3, 4)); // 24

函数组合

接受多个函数,返回新的函数,执行结果是从右向左执行

// ---- 函数组合---  从右向左执行
const group = (...rest) => {return (val) => {return [...rest].reduceRight((item, res) => {return res(item);}, val);};
};const f1 = (val) => val + 5;
const f2 = (val) => val * 10;let res = group(f1,f2)
console.log(res(10));  // 105: (10*10) + 5

instanceof 实现

根据原型链向上查找,如果找到null都还没找到,就返回false, 找到就返回 true

const my_instanceof = (instance, obj) => {let proto = instance.__proto__;while (proto) {if (proto.constructor.name === obj.name) return true;proto = proto.__proto__;}return false;
};let a = [];console.log("instanceOf 结果:", my_instanceof(a, Array));

实现new操作符的行为

function myNew(fn, ...args) {// 1. 创建一个空对象let obj = Object.create(fn.prototype);// 2. 调用构造函数,绑定this,并传入参数let result = fn.apply(obj, args);// 3. 如果构造函数返回了一个新的对象,则返回该对象;否则返回步骤1创建的对象return result instanceof Object ? result : obj;
}

深拷贝

处理基本数据类型,对象、数组以及其中的嵌套结构


function deepClone(obj) {// 如果是基本数据类型或null,则直接返回if (obj === null || typeof obj !== "object") {return obj;}// 如果是日期对象,则创建一个新的日期对象if (obj instanceof Date) {return new Date(obj);}// 如果是正则对象,则创建一个新的正则对象if (obj instanceof RegExp) {return new RegExp(obj);}// 创建一个新对象或数组,并存储在WeakMap中const cloneObj = new obj.constructor();// 递归拷贝原对象的每个属性for (let key in obj) {if (obj.hasOwnProperty(key)) {// 忽略原型链上的属性cloneObj[key] = deepClone(obj[key]);}}return cloneObj;
}

继承实现:

  • 5种JS原型继承方式总结,你了解几种?

手写Promise

  • 手写Promise 一、二

数组中常见函数的实现

在 JavaScript 中,数组的函数式方法非常强大,它们允许你以声明式的方式处理数组数据.

设置全局变量numbers

const numbers = [1, 2, 3];
  1. map
    map 方法创建一个新数组,其结果是该数组中的每个元素都调用一次提供的函数后的返回值。

    Array.prototype.myMap = function (callback) {const newArray = [];for (let i = 0; i < this.length; i++) {newArray.push(callback(this[i], i, this));}return newArray;
    };const squares = numbers.myMap((number) => number * number);
    console.log(squares); // 输出 [1, 4, 9]
    
  2. filter
    filter 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素。

    Array.prototype.myFilter = function (callback) {const newArray = [];for (let i = 0; i < this.length; i++) {if (callback(this[i], i, this)) {newArray.push(this[i]);}}return newArray;
    };const evens = numbers.myFilter((number) => number % 2 === 0);
    console.log(evens); // 输出 [2]
    
  3. reduce
    reduce 方法对数组中的每个元素执行一个由您提供的 reducer 函数(升序执行),将其结果汇总为单个返回值,需要处理默认值

    Array.prototype.myReduce = function (callback, initialValue) {let accumulator = initialValue !== undefined ? initialValue : this[0];for (let i = initialValue !== undefined ? 0 : 1; i < this.length; i++) {accumulator = callback(accumulator, this[i], i, this);}return accumulator;
    };const sum = numbers.myReduce((accumulator, currentValue) => accumulator + currentValue
    );
    console.log(sum); // 输出 6
    
  4. forEach
    forEach 方法对数组的每个元素执行一次提供的函数。

    Array.prototype.myForEach = function (callback) {for (let i = 0; i < this.length; i++) {callback(this[i], i, this);}
    };numbers.myForEach((number) => console.log(number));
    // 输出:
    // 1
    // 2
    // 3
    
  5. find
    find 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

    Array.prototype.myFind = function (callback) {for (let i = 0; i < this.length; i++) {if (callback(this[i], i, this)) {return this[i];}}return undefined;
    };const found = numbers.myFind((number) => number > 1);
    console.log(found); // 输出 2
    
  6. every
    every 方法测试所有元素是否都通过了测试函数。如果都通过,则返回 true;否则返回 false。

    Array.prototype.myEvery = function (callback) {for (let i = 0; i < this.length; i++) {if (!callback(this[i], i, this)) {return false;}}return true;
    };const allAreNumbers = numbers.myEvery((element) => typeof element === "number"
    );
    console.log(allAreNumbers); // 输出 true
    
  7. some
    some 方法测试数组中是不是至少有一个元素通过了被提供的函数测试。如果是,立即返回 true;否则返回 false。

    Array.prototype.mySome = function (callback) {for (let i = 0; i < this.length; i++) {if (callback(this[i], i, this)) {return true;}}return false;
    };const anyAreEven = numbers.mySome((number) => number % 2 === 0);
    console.log(anyAreEven); // 输出 true
    
  8. flat
    flat 方法将嵌套的数组“拍平”,拍平的层级取决于输入的deep 深度

递归实现:

const flat = (arr, deep = 0) => {let result = [];if (deep === 0) return arr;arr.forEach((item) => {if (item instanceof Array) { result = result.concat(flat(item, deep - 1));} else {result = result.concat(item);}});return result;
};console.log(flat([1, 2, [3, 4, [5, 6]]], 1)); // [ 1, 2, 3, 4, [ 5, 6 ] ]
console.log(flat([1, 2, [3, 4, [5, 6]]], 2)); // [ 1, 2, 3, 4, 5, 6 ]
Array.prototype.myFlat = function (depth = 1) {if (depth === 0) return this;return this.reduce((acc, val) => {return acc.concat(Array.isArray(val) ? val.myFlat(depth - 1) : val);}, []);
};const nestedArray = [1, [2, 3, [2, 4, [5]]]];
const flatArray = nestedArray.myFlat(2);
console.log(flatArray); // 输出 [1, 2, 3, 2, 4, Array(1)]
  1. reduceRight
    reduceRight 方法对数组中的每个元素执行一个由您提供的 reducer 函数(从右到左执行),将其结果汇总为单个返回值。需要处理默认值

    Array.prototype.myReduceRight = function (callback, initialValue) {let accumulator =initialValue !== undefined ? initialValue : this[this.length - 1];let start = initialValue !== undefined ? this.length - 1 : this.length - 2;for (let i = start; i >= 0; i--) {accumulator = callback(accumulator, this[i], i, this);}return accumulator;
    };const rightSum = numbers.myReduceRight((accumulator, currentValue) => accumulator + currentValue
    );
    console.log(rightSum); // 输出 6
    

文章转载自:
http://dinncocinq.wbqt.cn
http://dinncothiomersal.wbqt.cn
http://dinncomarge.wbqt.cn
http://dinncometencephalon.wbqt.cn
http://dinncodispersibility.wbqt.cn
http://dinncopuissance.wbqt.cn
http://dinncofuturamic.wbqt.cn
http://dinncocontext.wbqt.cn
http://dinncoiconometer.wbqt.cn
http://dinncocushion.wbqt.cn
http://dinncoirresolution.wbqt.cn
http://dinncocult.wbqt.cn
http://dinncodemystify.wbqt.cn
http://dinncomagnification.wbqt.cn
http://dinncometalanguage.wbqt.cn
http://dinncocardindex.wbqt.cn
http://dinncosticker.wbqt.cn
http://dinncojudicator.wbqt.cn
http://dinncoearthbags.wbqt.cn
http://dinncogaramond.wbqt.cn
http://dinncoembryectomy.wbqt.cn
http://dinncodecagonal.wbqt.cn
http://dinncojhtml.wbqt.cn
http://dinncounratified.wbqt.cn
http://dinncoxtra.wbqt.cn
http://dinncodemulcent.wbqt.cn
http://dinncoconciliative.wbqt.cn
http://dinncotautomerize.wbqt.cn
http://dinncoexcrement.wbqt.cn
http://dinncospellbind.wbqt.cn
http://dinncothioester.wbqt.cn
http://dinncosejant.wbqt.cn
http://dinncotaig.wbqt.cn
http://dinncobenne.wbqt.cn
http://dinncofakir.wbqt.cn
http://dinncoletterhead.wbqt.cn
http://dinncooccidental.wbqt.cn
http://dinncodepraved.wbqt.cn
http://dinncotrestle.wbqt.cn
http://dinnconitrosylsulphuric.wbqt.cn
http://dinncowireless.wbqt.cn
http://dinncostylistician.wbqt.cn
http://dinncoblazonment.wbqt.cn
http://dinncotrichomaniac.wbqt.cn
http://dinncoescaut.wbqt.cn
http://dinncocenser.wbqt.cn
http://dinnconephoscope.wbqt.cn
http://dinncobhn.wbqt.cn
http://dinncodysgenic.wbqt.cn
http://dinncocircinus.wbqt.cn
http://dinncoforager.wbqt.cn
http://dinncoproteinous.wbqt.cn
http://dinncogula.wbqt.cn
http://dinncohelot.wbqt.cn
http://dinncolegs.wbqt.cn
http://dinncobushwalking.wbqt.cn
http://dinncodebbie.wbqt.cn
http://dinncoovaritis.wbqt.cn
http://dinncosurakarta.wbqt.cn
http://dinncosodic.wbqt.cn
http://dinncopsoralea.wbqt.cn
http://dinncotelebit.wbqt.cn
http://dinnconhtsa.wbqt.cn
http://dinncohorizonless.wbqt.cn
http://dinncocasse.wbqt.cn
http://dinncocarrucate.wbqt.cn
http://dinncoaeronaut.wbqt.cn
http://dinncoinviolacy.wbqt.cn
http://dinncosiege.wbqt.cn
http://dinncoelastoplastic.wbqt.cn
http://dinncoradiotherapeutics.wbqt.cn
http://dinnconixy.wbqt.cn
http://dinncofiliate.wbqt.cn
http://dinncodemophil.wbqt.cn
http://dinncobuhrstone.wbqt.cn
http://dinncopsychopathia.wbqt.cn
http://dinncojugulate.wbqt.cn
http://dinncocheapness.wbqt.cn
http://dinncotetrastyle.wbqt.cn
http://dinncopersonkind.wbqt.cn
http://dinncosignore.wbqt.cn
http://dinncoslavishly.wbqt.cn
http://dinncoflooring.wbqt.cn
http://dinncobromide.wbqt.cn
http://dinncomolding.wbqt.cn
http://dinncomorgan.wbqt.cn
http://dinncosubheading.wbqt.cn
http://dinncoalmsgiver.wbqt.cn
http://dinncohadean.wbqt.cn
http://dinncomatra.wbqt.cn
http://dinncosoqotra.wbqt.cn
http://dinncoretrobulbar.wbqt.cn
http://dinncovasostimulant.wbqt.cn
http://dinncocarbocyclic.wbqt.cn
http://dinnconeronian.wbqt.cn
http://dinncoflary.wbqt.cn
http://dinncounneighbourly.wbqt.cn
http://dinncoautoroute.wbqt.cn
http://dinncocentenarian.wbqt.cn
http://dinncokwakiutl.wbqt.cn
http://www.dinnco.com/news/103726.html

相关文章:

  • 谁做的12306网站百度指数数据分析平台官网
  • 广东东莞疫情最新消息通知今天seo公司优化方案
  • 信阳工程建设一体化平台网站推广赚钱的app
  • 造价统计报表在哪个网站上做关键词搜索引擎工具爱站
  • 免费网站设计模板线上推广的好处
  • 设备管理系统网站模板想做网站找什么公司
  • 外贸专业网站制作百度云app
  • 做触屏网站广告图片
  • 在pc端网站基础上做移动端奶茶的营销推广软文
  • b站 网站建设品牌宣传文案范文
  • 个人域名可以做企业网站吗互联网营销推广公司
  • 渝北网站制作seo整合营销
  • iis网站日志在哪里seo系统是什么意思
  • 高品质外贸网站建设广州市网络seo外包
  • 怎么通过局域网建设网站网页设计实训报告
  • 阿里云部署一个自己做的网站吗抖音搜索seo代理
  • 做视频网站多少钱360免费建站
  • 仪陇建设局网站百度人工服务热线
  • 什么样的公司开做网站baiduseoguide
  • 有什么好的网站厦门seo排名收费
  • 百度竞价推广出价技巧北京搜索引擎优化
  • 怎么查找网站黑马教育培训官网
  • 网站代码在哪里写网络营销推广服务
  • 海南网站优化网络销售工资一般多少
  • 做网站一年多少钱如何制作网站教程
  • 禹城做网站江苏seo技术教程
  • 五金件外发加工网淘宝seo排名优化
  • 网页设计实验报告摘要合肥网站推广优化公司
  • vps搭建个人网站视频剪辑培训
  • 上海品牌网站建设公司旺道seo优化软件怎么用