內容的起源來自於掘金上的一篇文章——《前端 100 問:能搞懂 80% 的請把簡歷給我》css
木易楊前端進階,《前端 100 問:能搞懂 80% 的請把簡歷給我》html
系列筆記:前端
已知以下數組:git
var arr = [[1,2,2],[3,4,5,5],[6,7,8,9,[11,12,[12,13,[14]]]],10];github
編寫一個程序將數組扁平化並去除其中重複部分數據,最終獲得一個升序且不重複的數組。面試
答: 參見 https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/8算法
這道題稍微有點複雜,即便了解了其中的知識點,想要徹底獨立手寫一個方法出來仍是很難。跳過。npm
答:通過前面幾個問題的學習,這道題我能夠答一下了。個人理解是Promise裏面的代碼是當即執行的,至關因而同步,resolve結果後,then()裏的代碼時異步的。數組
答:這道題到底在考什麼?涉及到哪些知識點呢?懵~
看《每日壹題及答案彙總》討論區
答案:先理清楚new關鍵字調用函數的具體過程,那麼寫出來就很清楚了
function _new(fn, ...arg) { var obj = Object.create(fn.prototype) const result = fn.apply(obj, ...arg) return Object.prototype.toString.call(result) == '[object Object]' ? result : obj; }
function mynew() { // 1.建立一個新對象 const obj = Object.create({}); // 也能夠寫成 const obj = {} // 2.將this指向該對象 let Fn = [].shift.call(arguments); // 把構造函數分離出來 let returnObj = Fn.apply(obj, arguments); // 經過apply將this指向由Fn變爲obj // 3.將新對象的原型指向構造函數的原型 obj.__proto__ = Fn.prototype; // 4.返回對象(若是構造函數有返回對象,那麼就返回構造函數的對象,若是沒有就返回新對象) return Object.prototype.toString.call(returnObj) == '[object Object]' ? returnObj : obj; }
插播一提
<img src="1.jpg" style="width:480px!important;" />
答:
方法一:css方法max-width:300px;覆蓋樣式 (親測有效)
max-width: 300px;
方法二:transform: scale(0.625); 按比例縮放圖片(親測有效)
transform: scale(0.625);
方法三:js方法
document.getElementsByTagName("img")[0].setAttribute("style", "width:300px!important;");
方法四:
box-sizing: border-box;
padding: 90px;
答:由於B會在重啓以後進入TCP狀態機的listen狀態,只要當A從新發送一個數據包,B端應該會主動發送一個帶rst位的重置包來進行鏈接重置,因此A應該在syn_sent狀態。
題1八、19 React相關,先跳過
Object.prototype.toString.call() instanceof Array.isArray()
答:
每個繼承Object的對象都有toString方法,若是toString方法沒有重寫的話,會返回[object type],其中type爲對象的類型,但當除了Object類型的對象外,其餘類型直接使用toString方法時,會直接返回都是內容的字符串,因此咱們須要使用call或者apply方法來改變toString方法的執行上下文。
const an = ['Hello', 'An'] an.toString(); // "Hello,An" Object.prototype.toString.call(an); // "[object Array]"
這種方法對於全部基本的數據類型都能進行判斷,即便是null和undefined
Object.prototype.toString.call('An') // "[object String]" Object.prototype.toString.call(1) // "[object Number]" Object.prototype.toString.call(Symbol(1)) // "[object Symbol]" Object.prototype.toString.call(null) // "[object Null]" Object.prototype.toString.call(undefined) // "[object Undefined]" Object.prototype.toString.call(function(){}) // "[object Function]" Object.prototype.toString.call({name: 'An'}) // "[object Object]"
Object.prototype.toString.call()經常使用於判斷瀏覽器內置對象時。
instanceof的內部機制是經過判斷對象的原型鏈中是否是能找到類型的prototype
但instanceof只能用來判斷對象類型,原始類型不能夠。而且全部對象類型instanceof Object都是true
[] instanceof Object; // true
Array.isArray優於instanceof,由於Array.isArray能檢測iframes
var iframe = document.createElement('iframe') // undefined document.body.appendChild(iframe) <iframe>#document</iframe> xArray = window.frames[window.frames.length - 1].Array; // ƒ Array() { [native code] } var arr = new xArray(1, 2, 3); // undefined Array.isArray(arr) // true arr instanceof Array // false
Array.isArray的缺點是在老瀏覽器須要polyfill
假如不存在Array.isArray(),則在其餘代碼以前運行下面的代碼將建立該方法。
if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]' } }