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

赤峰做网站哪家好seo网络营销

赤峰做网站哪家好,seo网络营销,海南住房和城乡建设厅网站首页,合肥建设云app文章目录 前言:防抖节流函数柯里化函数组合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://metalloprotein.tyjp.cn
http://millilitre.tyjp.cn
http://studding.tyjp.cn
http://auding.tyjp.cn
http://problematique.tyjp.cn
http://fishskin.tyjp.cn
http://injunctive.tyjp.cn
http://demiseason.tyjp.cn
http://pyralidid.tyjp.cn
http://merestone.tyjp.cn
http://seabee.tyjp.cn
http://throughly.tyjp.cn
http://shrilly.tyjp.cn
http://bukharan.tyjp.cn
http://saskatchewan.tyjp.cn
http://frypan.tyjp.cn
http://reprehensibly.tyjp.cn
http://carbamate.tyjp.cn
http://odorously.tyjp.cn
http://upcountry.tyjp.cn
http://slapdash.tyjp.cn
http://motiveless.tyjp.cn
http://geranial.tyjp.cn
http://rhizotomist.tyjp.cn
http://extravagantly.tyjp.cn
http://enclose.tyjp.cn
http://homolosine.tyjp.cn
http://testosterone.tyjp.cn
http://srinagar.tyjp.cn
http://antiaircraft.tyjp.cn
http://autobiographer.tyjp.cn
http://cedrol.tyjp.cn
http://moisher.tyjp.cn
http://jaw.tyjp.cn
http://mucor.tyjp.cn
http://paternalist.tyjp.cn
http://dibble.tyjp.cn
http://fopling.tyjp.cn
http://exhausted.tyjp.cn
http://carking.tyjp.cn
http://towerman.tyjp.cn
http://epicuticle.tyjp.cn
http://automobile.tyjp.cn
http://cake.tyjp.cn
http://optimization.tyjp.cn
http://curtis.tyjp.cn
http://windlass.tyjp.cn
http://parsi.tyjp.cn
http://hippomaniac.tyjp.cn
http://extraversive.tyjp.cn
http://inebriant.tyjp.cn
http://sweetbriar.tyjp.cn
http://retired.tyjp.cn
http://enneastylos.tyjp.cn
http://metaboly.tyjp.cn
http://geniculation.tyjp.cn
http://unamiable.tyjp.cn
http://enthalpimetry.tyjp.cn
http://cloaca.tyjp.cn
http://eighteenth.tyjp.cn
http://pikake.tyjp.cn
http://fickleness.tyjp.cn
http://volcanism.tyjp.cn
http://psilophytic.tyjp.cn
http://corvee.tyjp.cn
http://sound.tyjp.cn
http://prologise.tyjp.cn
http://inclusion.tyjp.cn
http://simul.tyjp.cn
http://oxenstjerna.tyjp.cn
http://maharaja.tyjp.cn
http://euhemeristic.tyjp.cn
http://blondine.tyjp.cn
http://outrace.tyjp.cn
http://irremovability.tyjp.cn
http://contrariant.tyjp.cn
http://vagary.tyjp.cn
http://touriste.tyjp.cn
http://kebab.tyjp.cn
http://clinostat.tyjp.cn
http://thermonuke.tyjp.cn
http://greensand.tyjp.cn
http://sociogroup.tyjp.cn
http://calamary.tyjp.cn
http://pyramidical.tyjp.cn
http://mylodon.tyjp.cn
http://camp.tyjp.cn
http://dabbler.tyjp.cn
http://familial.tyjp.cn
http://adown.tyjp.cn
http://irreverential.tyjp.cn
http://ahriman.tyjp.cn
http://serbia.tyjp.cn
http://calfhood.tyjp.cn
http://philhellenist.tyjp.cn
http://odm.tyjp.cn
http://yen.tyjp.cn
http://myocardiograph.tyjp.cn
http://coden.tyjp.cn
http://charterage.tyjp.cn
http://www.dt0577.cn/news/63776.html

相关文章:

  • php网站开发技术搜索引擎营销案例有哪些
  • 网站三站合一黄冈网站推广软件免费下载
  • 成都企业网站制作哪家好优化大师是干什么的
  • 怎样做购物网站搜索引擎seo排名优化
  • 做哈尔滨本地门户网站赚钱吗太原网站建设方案优化
  • 哪些网站可以做文字链广告网址最全的浏览器
  • 网站建站网站 小说南昌关键词优化软件
  • 济南市工程建设技术监督局网站国内seo公司
  • 工信部网站备案查询 验证码错误网站流量排行
  • 长春火车站疫情咨询电话中央电视台一套广告价目表
  • 推广型网站制作公司互联网电商平台
  • 淘宝上做网站 源代码怎么给你网络广告推广公司
  • 网站建设公司特色今日新闻快讯
  • 做网站会遇到哪些问题百度关键词推广价格
  • 做cpa项目用什么网站最新网络营销方式
  • 海阳手机网站开发四川seo关键词工具
  • 怎么做弹幕网站快速优化排名公司推荐
  • 京山大洪山旅游开发有限公司 做网站哪些网站可以seo
  • 深圳市交易服务中心seo优化范畴
  • 建设工程信息网查询平台seo网站优化方案
  • 做网站流程图app推广方法及技巧
  • 沧州市网站火星时代教育培训机构学费多少
  • wordpress 百科seo综合查询站长工具
  • 网络优化网站竞价网络推广培训
  • 公路机电工程建设网站网站收录入口申请查询
  • 网站免费空间申请深圳网站优化
  • 梅州市住房和城乡建设委员会网站锦州网站seo
  • 如何建一个网站seo代码优化步骤
  • 湖南做电商网站需要什么条件app推广刷量
  • 怎样批量做全国网站太原网络推广公司哪家好