【题目1】
第一种写法
function changeName(name){ return name.replace(new RegExp(/[A-Z]/, 'g'), (str) => { return '_' + String.fromCharCode(str.charCodeAt(0) | 32); }); }
|
第二种写法
function changeName(name){ return name.replace(new RegExp(/[A-Z]/, 'g'), (str) => { return `_${str.toLowerCase()}`; }); }
|
【题目2】
第一种写法
function getMaxValue(array){ return array.sort((a, b) => { return b - a; })[0]; }
|
第二种写法 //ES5
function getMaxValue(array){ return Math.max.apply(null,array); }
|
第三种写法 //ES6
function getMaxValue(array){ return Math.max(...array); }
|
【题目3】
function flatten(arr){ return arr.reduce((pre, cur) => { return !Array.isArray(cur) ? [...pre,cur] : [...pre,...flatten(cur)]; },[]) }
|
【题目4】
export class EventEmitter { event: { [any: string]: { func: Function, one: Boolean }[] }; constructor() { this.event = {}; }
on(eventName: string, func: Function, one?: Boolean) { if (this.event[eventName]) { this.event[eventName].push({ func, one }); } else { this.event[eventName] = [{ func, one }]; } }
one(eventName: string, func: Function) { this.on(eventName, func, true) }
off(eventName: string) { this.event[eventName] = []; }
emit(eventName: string, ...args: any[]) { if (!this.event[eventName]) return; this.event[eventName].forEach((item, index, array) => { item.func(...args); if (item.one) { array.splice(index, 1); } }); } }
export default new EventEmitter();
|
【题目5】
var genCssSelector = function (dom) { let res = []; while(dom.parentNode){ if(dom.id != ''){ res.push('#' + dom.id); } else if(dom.className != ''){ let classname = ''; for (let i = 0, l = dom.classList.length; i < l; i++) { classname += '.' + dom.classList[i] } res.push(classname); }else{ res.push(dom.localName); } dom = dom.parentNode; } return res.reverse().join(' '); }
|
本博客遵循署名 4.0 协议国际版 (CC BY 4.0)协议
本文链接:https://xuefeng.is-a.dev/archives/classmate-Alibaba-interview
(●'◡'●)
如果你觉得不错或者对你有帮助,
你可以替我买一杯咖啡 ☕
If you think it's good or helpful,
you can buy me a cup of coffee ☕
Ailpay
Wechat