当前位置:网站首页>Handwritten common interview questions
Handwritten common interview questions
2022-06-12 10:37:00 【dralexsanderl】
Handwritten common interview questions
Handwritten common interview questions
Shake proof
Anti shake function principle : When the event is triggered n Seconds before the callback , If in this n It's triggered in seconds , Then the time will be counted again .
There are two situations :
- Click to execute immediately
- Click not to execute immediately
// Non immediate execution
const debounce1 = (fn, delay) => {
let timer = null;
return (...args) => {
if(timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay)
}
}
// Execute now
const debounce2 = (fn, delay) => {
let timer = null;
let emitNow = true;
return (...args) => {
if(timer) clearTimeout(timer);
if(emitNow) {
fn.apply(this, args);
emitNow = false;
} else {
timer = setTimeout(() => {
fn.apply(this, args);
emitNow = true;
}, delay)
}
}
}
// Whether to execute immediately is controlled by parameters
const debounce3 = (fn, delay, isImmediate) => {
let timer = null;
let emitNow = true;
return (...args) => {
if(timer) clearTimeout(timer);
if(isImmediate) {
if(emitNow) {
fn.apply(this, args);
emitNow = false;
} else {
timer = setTimeout(() => {
fn.apply(this, args);
emitNow = true;
}, delay)
}
} else {
timer = setTimeout(() => {
fn.apply(this, args);
}, delay)
}
}
}
throttle
Anti shake function principle : Within one unit time , Function can only be triggered once . If multiple functions are triggered in this unit time , Only once .
There are two situations :
- Click to execute immediately
- Click not to execute immediately
// Non immediate execution
const throttle1 = (fn, delay) => {
let isEmit = false;
return (...args) => {
if(isEmit) return;
isEmit = true;
setTimeout(() => {
fn.apply(this, args);
isEmit = false;
}, delay);
}
}
// Execute now
const throttle2 = (fn, delay) => {
let isEmit = false;
return (...args) => {
if(isEmit) return;
isEmit = true;
fn.apply(this,args);
setTimeout(() => {
isEmit = false;
},delay);
}
}
// Whether to execute immediately is controlled by parameters
const throttle3 = (fn, delay, isImmediate) => {
let isEmit = false;
return (...args) => {
if(isEmit) return;
isEmit = true;
if(isImmediate) {
fn.apply(this, args);
setTimeout(() => {
isEmit = false;
},delay);
} else {
setTimeout(() => {
fn.apply(this, args);
isEmit = false;
}, delay);
}
}
}
A deep clone
function deepCopy(obj) {
if(typeof obj !== 'object') {
return obj;
};
let cloneObj = obj.constructor=== Array ? [] : {
};
for(let property in obj) {
cloneObj[property] = typeof obj[property] === 'object' ? deepCopy(obj[property]) : obj[property];
}
return cloneObj;
}
instanceOf
according to Prototype chain Knowledge , We can soon know that according to the object __proto__ Property to find its constructor .
const instanceOf = function(object, target) {
// Take the prototype object of the target
const instance = target.prototype;
// Take the implicit prototype of the object to be tested
object = object.__proto__;
while(true) {
if(!object) return false;
if(object === instance) return true;
object = object.__proto__;
}
}
new The operator
new The role of :
- Create a new object
- take
thisExecute the new object created - The new object created will be linked to the function
prototypeOn the object ( New object__proto__Property points to the functionprototype); - Using function call Method , Point the original to window Binding object of this Yes obj.( thus , When we pass the arguments to the function again , The properties of the object are mounted to obj On .)
function createObject() {
// Create a new object
const obj = {
};
// Get constructor , use call The method makes arguments Able to use shift Method takes the first parameter ( Constructors ) Take it out
const Constructor = [].shift.call(arguments);
// Put the object __proto__ Property is linked to the constructor prototype Properties of the
obj.__proto__ = Constructor.prototype;
// In the constructor this Point to an object and pass arguments
const result = Constructor.apply(obj, arguments);
// Make sure the return value is an object
return typeof ret === "object" ? result : obj;
}
Realization call Method
We all know call This method is used to modify this Point to , But some students may not understand the principle , Let's write one call Methods help to gain insight into how it works .
Function.prototype.mycall = function(context) {
// The default context is window
context = context || window;
// Add a property to hold the current call call Function of
context.fn = this;
// take arguments Convert to an array and remove the first parameter ( Context )
const args = [...arguments].slice(1);
// When a function is called in this way, the function's internal this It points to the caller (context);
const result = context.fn(...args);
delete context.fn;
return result;
}
Realization apply Method
apply Principle and call Very similar , The only difference is the problem of parameter transmission ,apply The second parameter of the method is an array of all the parameters , and call Method except that the first parameter is context Outside , Other parameters are passed in .
Function.prototype.myapply = function(context, arr) {
// The default context is window
context = context || window;
// Add a property to hold the current call call Function of
context.fn = this;
// take arguments Convert to an array and remove the first parameter ( Context )
let result;
if(!arr) {
result = context.fn();
} else {
result = context.fn(arr);
}
delete context.fn;
return result;
}
Realization bind Method
be relative to call and apply for ,bind The return value of the method is a change this Function of ( That is, it is not called immediately ). When the returned function is used as a constructor ,this invalid , But the parameters passed in are still valid .
bind() Method creates a new function . When this new function is called ,bind() The first parameter of will be the this, The following sequence of parameters will be passed in as its parameters before the passed arguments .
Function.prototype.mybind = function(context) {
if(typeof this !== 'function') {
throw new Error('Uncaught TypeError: not a function')
}
const args = [...arguments].slice(1);
// Used to record the current incoming function prototype;
let Transit = function() {
};
const _ = this;
const FunctionToBind = function() {
const bindArgs = [...arguments];
return _.apply(this instanceof Transit ? this : context, args.concat(bindArgs));
}
// Record the... Of the currently passed in function prototype;
Transit.prototype = this.prototype;
FunctionToBind.prototype = new Transit();
return FunctionToBind;
}
Realization Object.create Method
Object.create() Method creates a new object , Using an existing object to provide a newly created object __proto__.
grammar :
Object.create(proto[, propertiesObject])proto: must . Represents the prototype object of the new object , That is, the parameter will be assigned to the target object ( That is, the new object , Or the last returned object ) On the prototype of . The parameter can benull, object , Functionalprototypeattribute ( When creating an empty object, you need to passnull, Otherwise it will throwTypeErrorabnormal )propertiesObject: Optional . Enumerable properties added to the newly created object ( That is, its own properties , Instead of enumerating properties on the prototype chain ) The property descriptor of the object and the corresponding property name . These attributes correspond toObject.defineProperties()Second parameter of , The default attribute descriptor for creating non empty objects isfalseOf , The descriptor of an object property created by a constructor or literal method is assumed by default to betrue.
new The key word is to create objects through constructors , The added attribute is under its own instance .Object.create() Another way to create objects , It can be understood as inheriting an object , The added attribute is under the prototype .
// new Object() Way to create
var a = {
rep : 'apple' }
var b = new Object(a)
console.log(b) // {rep: "apple"}
console.log(b.__proto__) // {}
console.log(b.rep) // {rep: "apple"}
// Object.create() Way to create
var a = {
rep: 'apple' }
var b = Object.create(a)
console.log(b) // {}
console.log(b.__proto__) // {rep: "apple"}
console.log(b.rep) // {rep: "apple"}
So much for that Object.create Knowledge , Let's implement this method :
Object.prototype.mycreate = function(proto, propertiesObject) {
function F() {
};
F.prototype = proto;
const obj = new F();
if(propertiesObject) {
Object.defineProperties(obj, propertiesObject);
}
return obj
}
The implementation principle is to create an empty constructor and prototype Point to the incoming object , Finally, an instance of the constructor is returned .
Realization promise
const statusMap = {
PENDING: "pending",
FULFILLED: "fulfilled",
REJECTED: "rejected"
}
class MyPromise{
constructor(handler) {
if(Object.prototype.toString.call(handler) !== '[object Function]') {
throw new Error('the first parameter should be a function');
}
this.status = statusMap.PENDING;
this.result = null;
// Used to perform then Method
this.fulfilledQueues = [];
this.rejectedQueues = [];
try{
// Execute two methods
handler(this._resolve.bind(this), this._reject.bind(this));
} catch(err) {
this._reject(err);
}
}
_resolve(val) {
if(this.status !== statusMap.PENDING) return;
const run = () => {
this.status = statusMap.FULFILLED;
this.result = val;
let cb;
while(cb = this.fulfilledQueues.shift()) {
cb(val);
}
}
setTimeout(() => run(), 0);
}
_reject(err) {
if(this.status !== statusMap.PENDING) return;
const run = () => {
this.status = statusMap.REJECTED;
this.result = err;
let cb;
while(cb = this.rejectedQueues.shift()) {
cb(err);
}
}
setTimeout(() => run(), 0);
}
}
MyPromise.prototype.then = function(onFulfilled, onRejeceted) {
const {
status, result } = this;
return new MyPromise((onFulfilledNext, onRejecetedNext) => {
let fulfilled = value => {
try {
if(Object.prototype.toString.call(onFulfilled) !== '[object Function]') {
onFulfilledNext(value);
} else {
let res = onFulfilled(value);
// The return result is MyPromise Example
if(res instanceof MyPromise) {
res.then(onFulfilledNext, onRejecetedNext);
} else {
onFulfilledNext(res);
}
}
} catch(e) {
onRejecetedNext(e);
}
}
let rejected = error => {
try {
if(Object.prototype.toString.call(onRejeceted) !== '[Object Function]') {
onRejecetedNext(error);
} else {
let res = onRejeceted(error);
// The return result is MyPromise Example
if(res instanceof MyPromise) {
res.then(onFulfilledNext, onRejecetedNext);
} else {
onFulfilledNext(res);
}
}
} catch(e) {
onRejecetedNext(e);
}
}
switch(status) {
case statusMap.PENDING:
this.fulfilledQueues.push(fulfilled);
this.rejectedQueues.push(rejected)
break;
case statusMap.FULFILLED:
this.fulfilledQueues.push(fulfilled);
break;
case statusMap.REJECTED:
this.rejectedQueues.push(rejected);
break;
}
})
}
Flattening arrays
Array Methods flat Many browsers have not yet implemented , And the browser supports flat Method cannot handle nested arrays . Write a flat Method , Flat nested arrays .
// The simplest solution
Array.prototype.flat = function (arr) {
return arr
.toString()
.split(',')
.map((item) => +item);
};
Array.prototype.flat = function (arr) {
return arr.reduce((prev, item) => {
return prev.concat(Array.isArray(item) ? flatten(item) : item);
}, []);
};
Array weight removal
For removal 1 Repeat more than times item, have access to Set.
function delRepeat(arr) {
return Array.from(new Set(arr));
}
But remove 2 You can't use it more than times set 了 .
// Known array
var arr = [1,1,1,1,1,1,1,3,3,3,3,3,5,5];
// Method 1
function delRepeat(arr) {
arr = arr.sort();
for (let i = 0; i < arr.length; i++) {
if (arr[i] == arr[i + 2]) {
arr.splice(i, 1);
i--;
}
}
return arr;
}
// Method 2
function delRepeat(arr) {
var newArr = [];
var obj = {
};
arr.map((item) => {
if (obj[item]) {
obj[item] += 1;
} else {
obj[item] = 1;
}
obj[item] <= 2 ? newArr.push(item) : '';
});
return newArr;
}
Code address :https://github.com/leopord-lau/EasyPresentation
边栏推荐
- Wechat payment, wechat refund, Alibaba payment
- Mysql5.6.24 installation free deployment method
- Fiddler automatically saves the result of the specified request to a file
- file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
- See if you fall into the trap of "labeling" customers and users?
- Get start date and end date for all quarters of the year
- JS string combination
- CONDA install tensorflow test tensorflow
- PHP get (remote) large file method record
- golang中的定时器
猜你喜欢

Building 64 bit wampserver and DVWA in win7 virtual machine

On the improvement of 3dsc by harmonic shape context feature HSC

Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)

Valentina Studio Pro for MAC (MAC database management software)

Vite Basics

How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games

Win10 professional edition user name modification

使用cpolar远程办公(2)

MySQL user and permission management, role management

PHP download station B video
随机推荐
蓝桥杯2015年CA省赛(填坑中)
Remote desktop cannot copy and paste solution
Vscode code debugging skills
Simple use of autojs
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
PHP wechat payment V3 interface
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
对网络库协程的思考——读brpc有感
JS string combination
Assign a specified amount to a specified number of people at random
淘宝618超级喵运会怎么玩?超级喵运会整体活动攻略来了
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
MySQL user and permission management, role management
See if you fall into the trap of "labeling" customers and users?
力扣(LeetCode)162. 寻找峰值(2022.06.11)
Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
Mysql5.6.24 installation free deployment method
Pycharm view the current version of opencv
2022京东618预售定金怎么退?京东618定金能退吗?