QUIZ

13次阅读

共计 2440 个字符,预计需要花费 7 分钟才能阅读完成。

1. 实现 css 布局一个 div 垂直居中其距离屏幕左右两边各 10px 其高度始终是宽度的 50%
div 中有文本 ’A’ 其 font—size:20px 文本水平垂直居中
<style>
.wrapper {
margin: 0 10px;
display: flex;
align-items: center;
justify-content: center;
background: pink;
}
</style>

<body>
<div class=”wrapper”>
<div class=”font”>A</div>
</div>
</body>

ps: 经试验 其高度始终是宽度的 50% 这个没有实现
2. 函数中的 arguments 是数组吗?类数组转数组的方法了解一下?
arguments 类数组 var array = […arguments]
3. 类型比较
if([]==false){console.log(1)};if({}==false){console.log(2)};if([]){console.log(3)}if([1]==[1]){console.log(4)}
都不打印
4.EventLoop
async function a1 () {
console.log(‘a1 start’)
await a2()
console.log(‘a1 end’)
}
async function a2 () {
console.log(‘a2’)
}

console.log(‘script start’)

setTimeout(() => {
console.log(‘setTimeout’)
}, 0)

Promise.resolve().then(() => {
console.log(‘promise1’)
})

a1()

let promise2 = new Promise((resolve) => {
resolve(‘promise2.then’)
console.log(‘promise2’)
})

promise2.then((res) => {
console.log(res)
Promise.resolve().then(() => {
console.log(‘promise3’)
})
})
console.log(‘script end’)
打印:script starta1 starta2a1 endscript endpromise1promise2promise2.thenpromise3setTimeout
promise2 错了,知道为什么,但是不知道为啥 a1 end 是在异步代码执行后打印的
5. 改正代码,输出 0123401234
function a () {
for (var i = 0; i < 5; i++) {
this.i = i
setTimeout(function () {
console.log(i)
}, 0)
console.log(this.i)
}
}

a()
将 var 改成 let 考察闭包但是好像是错的 …. 为什么改成 let 会中间出现 undefined….. 改成 let 后:01234undefined01234
6. 手写 bind 实现
Function.prototype.bind2 = function (context) {

var self = this;
// 获得 bind 的参数从第二个参数到最后一个参数
var args = Array.prototype.slice.call(arguments, 1);

var fNOP = function () {};

var fBound = function () {
// 指 bind 返回的函数传入的参数
var bindArgs = Array.prototype.slice.call(arguments);
// new bind 返回的函数,this 失效,但传入的参数生效
return self.apply(this instanceof fNOP ? this : context, args.concat(bindArgs));
}
// 保证继承,原型链,下面两行代码等同于 Object.creater() fbound.prototype = Object.create(this.prototype);
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
}
7. 看这个图,我的理解是,持续触发事件,每隔一段时间,只执行一次事件,事件节流

function throttle(func, wait) {
var context, args;
var previous = 0;

return function() {
var now = +new Date();
context = this;
args = arguments;
if (now – previous > wait) {
func.apply(context, args);
previous = now;
}
}
}

// 调用
元素.onmousemove = throttle(func, 100);
8. 从一个无序,不相等的数组中,选取 N 个数,使其和为 M 实现算法算法题凉 ….. 我感觉这道题应该和二叉树有关 ……
9. 一个字典 [‘I’, ‘have’, ‘a’, ‘book’, ‘good’],实现一个函数,判断一个字符串中是否都是出自字典中的,输出 true/false 算法题凉 ….. 笨方法:
var arr = [‘I’, ‘have’, ‘a’, ‘book’, ‘good’]
var str = ‘I have a book’

function test(str,arr) {
return arr.filter(v => str.indexOf(v) !== -1).length === str.split(‘ ‘).length
}
10. 一个长阶梯有 n 级,可以一次走 1 级,一次走 2 级,一共有多少种走法?
function test (n) {
if (n === 1) return 1
if (n === 2) return 2
return test(n – 1) + test(n – 2)
}
用笨方法做的 …. 先写出来 n 为 1,2,3,4,5 时的走法,看出是用递归

正文完
 0