這份面試題與常見版本一致,圍繞三個核心能力展開:遍歷容器、面向對象封裝,以及 URL 解析。下面逐一說明每道題的要求與期望輸出,方便讀者對照練習。
作答要求:
1、只能在指定位置填寫代碼,本文件其他代碼不能修改
2、所有題目都不允許新增全局變量名
3、代碼需能在 Firebug 的 console 中正常執行並輸出結果
4、代碼最優化、效率最高
5、代碼註釋明確
實現 each(obj, fn),遍歷數組或對象的所有成員。回調中的 this 指向當前值:返回 true 跳過當前項,返回 false 中止整個遍歷。
var each = function(obj, fn){
//+++++++++++ 答題區域 +++++++++++
//+++++++++++ 答題結束 +++++++++++
};
對數組 [4,5,6,7,8,9,10,11,12] 遍歷,跳過 6、在 8 處中止,輸出:
1: "4" 2: "5" 4: "7"
對對象 {a:4, b:5, c:6} 遍歷,跳過值 5,輸出:
a: "4" c: "6"
構建一個名為 Man 的類,提供 attr、words、say 三個方法,且需同時支援 new Man(...) 與 Man(...) 兩種呼叫形式。
var Man; //+++++++++++ 答題區域 +++++++++++ //+++++++++++ 答題結束 +++++++++++
重點要求:
此題是閉包、原型與 this 綁定的綜合考驗。
實現 urlParser(s),把 URL 中 # 後的參數解析成指定數據結構。
function urlParser(s){
//+++++++++++ 答題區域 +++++++++++
//+++++++++++ 答題結束 +++++++++++
}
期望輸出:
urlParser("https://www.abc.com/m/s/#page/2/?type=latest_videos&page_size=20")
// ["page", "2", { "type": "latest_videos", "page_size": 20 }]
urlParser("https://www.abc.com/m/s/#type=latest_videos&page_size=20")
// [{ "type": "latest_videos", "page_size": 20 }]
urlParser("https://www.abc.com/m/s/#page?type=latest_videos&page_size=20")
// ["page", { "type": "latest_videos", "page_size": 20 }]
本題要求能靈活處理路徑段與查詢串的各種組合,是檢驗字符串處理功底的好題目。