100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 手记 《半年工作经验今日头条和美团面试题面经分享》

手记 《半年工作经验今日头条和美团面试题面经分享》

时间:2018-06-17 14:03:11

相关推荐

手记 《半年工作经验今日头条和美团面试题面经分享》

最近看到一篇文章《半年工作经验今日头条和美团面试题面经分享》,对于文章里出现的面试题参考各种资料写了一下,有写的不完善的地方希望大家友善讨论~

原文链接:半年工作经验今日头条和美团面试题面经分享 - 掘金

一、css和html

A元素垂直居中A元素距离屏幕左右各边各10pxA元素里的文字font—size:20px,水平垂直居中A元素的高度始终是A元素宽度的50% 复制代码

<html><head><title>测试css</title></head><body><div class="father"><div class="child">啊啊啊啊</div></div></body><style>.father {height: 100%;width: 100%;display: flex;align-items:center;text-align: center;background:#222;padding: 0 10px;}.child {margin: auto;width: 100%; padding-top: 25%; // width和padding百分比的基数 都是父元素的宽度padding-bottom: 25%;background:#eee;font-size: 20px;height: 0;}</style></html>复制代码

二、函数arguments

函数中的arguments是数组吗?怎么转数组?复制代码

一、 arguments是类数组, 拥有数组的index和length属性, 不能直接调用数组的方法二、 类数组转数组的几种方法1. Array.from(arguments)2. [...arguments]3. Array.prototype.slice.call(arguments) 或者 [].prototype.slice.call(arguments)复制代码

三、以下打印结果

if([]==false){console.log(1)};if({}==false){console.log(2)};if([]){console.log(3)}if([1]==[1]){console.log(4)}复制代码

结果: 输出1 3==两边Object类型会先转换为原始类型, 具体是通过valueOf()和toString()方法1. 左侧: [].valueOf还是[],调用toString()方法, [].toString为'',''为String类型,需要转换为Number,为0右侧: false为boolean类型,需要转换为number,为02. 左侧: 同上,{}.toString为'[object Object]',需要转换为Number,为NaN右侧:false为boolean类型,需要转换为number,为03. []转换为boolean类型为true4. 引用地址不同复制代码

四、以下输出结果

async function async1(){console.log('async1 start')await async2()console.log('async1 end')}async function async2(){console.log('async2')}console.log('script start')setTimeout(function(){console.log('setTimeout') },0) async1();new promise(function(resolve){console.log('promise1')resolve();}).then(function(){console.log('promise2')})console.log('script end')复制代码

script startasync1 startasync2promise1script endpromise2async1 endsetTimeout主要注意 await async2() 相当于 new Promise(()=>{console.log(async2)}).then((resolve) => {resolve()})复制代码

五、手写bind

注意构造函数不能使用箭头函数定义, this指向问题Function.prototype.binds = function (context) {if (typeof this !== "function") {throw new Error("Function.prototype.bind - what is trying to be bound is not callable");}let self = this;let beforeArgs = Array.prototype.slice.call(arguments, 1);let fNOP = function () {};let fbound = function () {let lastArgs = Array.prototype.slice.call(arguments);self.apply(this instanceof fNOP ? this : context, beforeArgs.concat(lastArgs));}fNOP.prototype = this.prototype;fbound.prototype = new fNOP(); // 使用空函数过渡, 修改新函数prototype时不会对原函数的prototype造成影响return fbound;}复制代码

六、节流函数

// fn是我们需要包装的事件回调, interval是时间间隔的阈值function throttle(fn, interval) {let last = 0return function () {let context = thislet args = argumentslet now = +new Date()if (now - last >= interval) {last = now;fn.apply(context, args);}}}复制代码

七、随意给定一个无序的、不重复的数组data,任意抽取n个数,相加和为sum,也可能无解,请写出该函数

/*** 获取数组arr内,num个数的全组合* 比如 arr = [1,2,3,4], num = 3* 返回 [[1,2,3], [1,2,4], [1,3,4], [2,3,4]]* @param {*} arr * @param {*} num */function getCombination(array, number) {let result=[];(function (group,arr,num){if (num == 0) {return result.push(group); }let length = arr.lengthfor (let i = 0; i <= length-num; i++) {arguments.callee(group.concat(arr[i]), arr.slice(i + 1), num - 1);}})([], array, number);return result;}/*** 随意给定一个无序的、不重复的数组data,任意抽取n个数,相加和为sum,也可能无解,请写出该函数* @param {*} arr * @param {*} num * @param {*} sum */function main(arr, num, sum) {let result = [];let list = getCombination(arr, num);for (let item of list) {let listSum = item.reduce((pre, current) => {return pre + current;},0)if (listSum == sum) {result.push(item);}}return result;}const arr = [-1,1,2,3,4,5]const num = 2;const sum = 6;const result = main(arr, num, sum);console.log(result)复制代码

参考文章:

JavaScript深入之bind的模拟实现 - 掘金翻译 | ES6 箭头函数使用禁忌 - iKcamp - SegmentFault 思否从给定的无序、不重复的数组 A 中,取出 N 个数,使其相加和 为 MJS的防抖与节流

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。