15个手写JS,巩固你的JS基础(面试高频)

}

return res;

}

当然也可以用include、filter,思路大同小异。

方法四:利用include

const unique3 = arr => {

const res = [];

for (let i = 0; i < arr.length; i++) {

if (!res.includes(arr[i])) res.push(arr[i]);

}

return res;

}

方法五:利用filter

const unique4 = arr => {

return arr.filter((item, index) => {

return arr.indexOf(item) === index;

});

}

方法六:利用Map

const unique5 = arr => {

const map = new Map();

const res = [];

for (let i = 0; i < arr.length; i++) {

if (!map.has(arr[i])) {

map.set(arr[i], true)

res.push(arr[i]);

}

}

return res;

}

03.类数组转化为数组


类数组是具有length属性,但不具有数组原型上的方法。常见的类数组有arguments、DOM操作方法返回的结果。

方法一:Array.from

Array.from(document.querySelectorAll(‘div’))

方法二:Array.prototype.slice.call()

Array.prototype.slice.call(document.querySelectorAll(‘div’))

方法三:扩展运算符

[…document.querySelectorAll(‘div’)]

方法四:利用concat

Array.prototype.concat.apply([], document.querySelectorAll(‘div’));

04.Array.prototype.filter()


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Array.prototype.filter = function(callback, thisArg) {

if (this == undefined) {

throw new TypeError(‘this is null or not undefined’);

}

if (typeof callback !== ‘function’) {

throw new TypeError(callback + ‘is not a function’);

}

const res = [];

// 让O成为回调函数的对象传递(强制转换对象)

const O = Object(this);

// >>>0 保证len为number,且为正整数

const len = O.length >>> 0;

for (let i = 0; i < len; i++) {

// 检查i是否在O的属性(会检查原型链)

if (i in O) {

// 回调函数调用传参

if (callback.call(thisArg, O[i], i, O)) {

res.push(O[i]);

}

}

}

return res;

}

对于>>>0有疑问的:解释>>>0的作用

05.Array.prototype.map()


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Array.prototype.map = function(callback, thisArg) {

if (this == undefined) {

throw new TypeError(‘this is null or not defined’);

}

if (typeof callback !== ‘function’) {

throw new TypeError(callback + ’ is not a function’);

}

const res = [];

// 同理

const O = Object(this);

const len = O.length >>> 0;

for (let i = 0; i < len; i++) {

if (i in O) {

// 调用回调函数并传入新数组

res[i] = callback.call(thisArg, O[i], i, this);

}

}

return res;

}

06.Array.prototype.forEach()


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

forEach跟map类似,唯一不同的是forEach是没有返回值的。

Array.prototype.forEach = function(callback, thisArg) {

if (this == null) {

throw new TypeError(‘this is null or not defined’);

}

if (typeof callback !== “function”) {

throw new TypeError(callback + ’ is not a function’);

}

const O = Object(this);

const len = O.length >>> 0;

let k = 0;

while (k < len) {

if (k in O) {

callback.call(thisArg, O[k], k, O);

}

k++;

}

}

07.Array.prototype.reduce()


在这里插入图片描述

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Array.prototype.reduce = function(callback, initialValue) {

if (this == undefined) {

throw new TypeError(‘this is null or not defined’);

}

if (typeof callback !== ‘function’) {

throw new TypeError(callbackfn + ’ is not a function’);

}

const O = Object(this);

const len = this.length >>> 0;

let accumulator = initialValue;

let k = 0;

// 如果第二个参数为undefined的情况下

// 则数组的第一个有效值作为累加器的初始值

if (accumulator === undefined) {

while (k < len && !(k in O)) {

k++;

}

// 如果超出数组界限还没有找到累加器的初始值,则TypeError

if (k >= len) {

throw new TypeError(‘Reduce of empty array with no initial value’);

}

accumulator = O[k++];

}

while (k < len) {

if (k in O) {

accumulator = callback.call(undefined, accumulator, O[k], k, O);

}

k++;

}

return accumulator;

}

08.Function.prototype.apply()


第一个参数是绑定的this,默认为window,第二个参数是数组或类数组

Function.prototype.apply = function(context = window, args) {

if (typeof this !== ‘function’) {

throw new TypeError(‘Type Error’);

}

const fn = Symbol(‘fn’);

context[fn] = this;

const res = contextfn;

delete context[fn];

return res;

}

09.Function.prototype.call


call唯一不同的是,call()方法接受的是一个参数列表

Function.prototype.call = function(context = window, …args) {

if (typeof this !== ‘function’) {

throw new TypeError(‘Type Error’);

}

const fn = Symbol(‘fn’);

context[fn] = this;

const res = contextfn;

delete context[fn];

return res;

}

10.Function.prototype.bind


Function.prototype.bind = function(context, …args) {

if (typeof this !== ‘function’) {

throw new Error(“Type Error”);

}

// 保存this的值

var self = this;

return function F() {

// 考虑new的情况

if(this instanceof F) {

return new self(…args, …arguments)

}

return self.apply(context, […args, …arguments])

}

}

11.debounce(防抖)


触发高频时间后n秒内函数只会执行一次,如果n秒内高频时间再次触发,则重新计算时间。

const debounce = (fn, time) => {

let timeout = null;

return function() {

clearTimeout(timeout)

timeout = setTimeout(() => {

fn.apply(this, arguments);

}, time);

}

};

防抖常应用于用户进行搜索输入节约请求资源,window触发resize事件时进行防抖只触发一次。

12.throttle(节流)


总结

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。


触发高频时间后n秒内函数只会执行一次,如果n秒内高频时间再次触发,则重新计算时间。

const debounce = (fn, time) => {

let timeout = null;

return function() {

clearTimeout(timeout)

timeout = setTimeout(() => {

fn.apply(this, arguments);

}, time);

}

};

防抖常应用于用户进行搜索输入节约请求资源,window触发resize事件时进行防抖只触发一次。

12.throttle(节流)


总结

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值