前端面試(手寫)

2021-10-07 03:01:10 字數 3031 閱讀 3403

手寫篇

手寫 instenceof

原生的 instanceof

console.log( instanceof array) // true

console.log(』』 instanceof array) // false

手寫 myinstanceof :

function myinstanceof(left,right)console.log(myinstanceof(,array))// true

console.log(myinstanceof(』』,array))// false

實現原理:

通過不斷的沿著原型鏈查詢,如果找到頂端了即: proto === null ,那麼就說明沒有找到,返回false,說明 left 不是 right 建構函式的例項

如果找到隱式原型 proto 等於建構函式的原型 prototype ,那麼說明 left 是 right 建構函式的例項,返回true

其它情況就是不斷的改變 proto ,以便可以不斷的往上查詢

手寫 flat

原生示例:

const arr1 = [1, 2, [3, 4]];

arr1.flat();

// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];

arr2.flat();

// [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];

arr3.flat(2);

// [1, 2, 3, 4, 5, 6]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];

arr4.flat(infinity);

// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

手寫 flatdeep:

function flatdeep( arr, dep=1 )else

}return ret}

實現原理:

第乙個引數是陣列,第二個是降維層級,

用for迴圈遍歷這個陣列,檢測每一項

如果這項是不是陣列則直接新增到 ret 結果陣列裡面

否則根據降維層級判斷,預設是降一維層級,當遞迴降維不滿足 ret>0 ,說明已經達到dep降維層數了,其它情況即 ret.push(arr[i])

手寫 call

function.prototype.mycall = function(context) 當指向 context.fn 時,say裡面的this 指向obj [關鍵]

//obj 此時變成 var obj = }

let args = [...arguments].slice(1) //擷取第二個開始的所有引數

let result= context.fn(...args)//把執行的結果賦予result變數

delete context.fn //刪除執行上下文上的屬性 (還原)由var obj = }刪除fn

return result}

var name = 『outername』

var obj =

function say()

say()//outername 等價於 window.say this指向window

say.mycall(obj)//innername

實現原理:

函式的原型方法call 第乙個引數是傳入的執行上下文,後面傳入的都是引數,以逗號隔開

當傳入的是null或undefined是執行上下文是指向 window ,否使為傳入的物件,然後再傳入的物件身上新增 fn 屬性並把函式例項say函式賦值給fn,此時變成

var obj = } 此時 context 就是obj物件啦,所有你執行 context.fn(…args)

其實就是 obj.fn(…args) , fn 其值是 function say() ,所以這個 this 就變成 obj 物件了

然後就是結果賦值,物件還原

返回結果

context =(context === null || context === undefined) ? window : context

let result

context.fn = this

result = arguments[1] ? context.fn(...arguments[1]) : context.fn()

delete context.fn

return result

一:如果第二個引數存在,執行的時候就把第二個引數(陣列形式)用擴充套件運算子打散後傳入執行

二:如果第二個引數不存在,執行執行

其它就於 call 的實現一樣

手寫 bind

function.prototype.mybind = function(context)

return fn}

bind 的手寫實現,與其它兩個區別是返回乙個函式,並沒返回函式執行的結果,並且受參形式不受限制

實現原理:

通過 object.create 方法建立乙個新物件,使用現有的物件來提供新建立的物件的proto,通過 中介物件 o 來實現,來達到不影響傳入的物件

手寫 new

new 乙個函式的時候,會生成乙個例項,該例項的隱式原型proto===該函式的 prototype 原型物件

在建構函式中this指向當前例項

最後再將例項物件返回

function mynew(func)

檢測:function m(){}

let m = mynew(m); // 等價於 new m 這裡只是模擬

console.log(m instanceof m); // instanceof 檢測例項

console.log(m instanceof object);

console.log(m.proto.constructor === m);

面試手寫方法

陣列 map array.prototype.map function fn filter array.prototype.filter function fn 扁平化 function arr return arr 純數字時使用,若其中有其他型別,則需另外判斷或另行他法 當然,最簡單的是 arr....

面試 手寫函式合集

函式作用 把src的字串放到dst的位置。char strcpy char dst,const char src 該函式是沒有考慮重疊的 char strcpy char dst,const char src else return res 函式作用 把src的字串的前size個字元放到dst的位置...

面試 手寫String類

include includeclass cmystring cmystring cmystring char pdata else 當cmystring cmtest2 cmtest1 時或cmystring cmtest2 cmtest1 時呼叫 cmystring cmystring cons...