1.以下css最后是什么颜色
<style>
.classA{
color:blue;
}
.classB{
color:red;
}
</style>
<p class="classB classA">123</p>
与样式定义在文件中的先后顺序有关,即是后面的覆盖前面的,与在<p class=’classB classA’>中的先后关系无关。
2.以下代码运行结果
Function.prototype.a = () => alert(1);
Object.prototype.b = () => alert(2);
function A() {}
const a = new A();
a.a(); // 报错
a.b(); // 2
3.以下代码运行结果
console.log(typeof typeof typeof null); // string
4.如下代码展示的时候在加上position属性和未加入position属性的时候,分别会怎么显示
<style>
p {
background: red;
color: #000;
/*
position: absolute;
*/
}
</style>
<div>
<span>11111</span>
<p>2222<p>
<span>3333</span>
</div>
不加absolute
加上
5.以下代码运行结果
var a = [1, 2, 3, 4];
function set(a) {
a = [5, 6, 7, 8];
}
set(a);
console.log(a); // [1,2,3,4] 函数传参按值传递
6.
var name = '123';
var obj = {
name: '456',
getName: function () {
function printName () {
console.log(this.name);
}
printName();
}
}
obj.getName(); // 123
7.
var a = { x: 1 };
var b = a;
a = { n: 1 };
console.log(a); // {n: 1}
console.log(b); // {x: 1}
8.
var count = 10;
function a() {
return count + 10;
}
function b() {
var count = 20;
return a();
}
console.log(b()); // 20
9.
setTimeout(() => {
setTimeout(() => {
console.log(1);
}, 0);
}, 0);
setTimeout(() => {
console.log(2);
}, 100);
// 1,2
10.
var a = 1;
function fn() {
console.log(a);
var a = 2;
}
fn(); // undefined 因为已经被fn中变量提升的var覆盖了,只是没有赋值
11. 实现 bind
12. 数组扁平化
// 1. 数组自带 flat(层数)
arr.flat(Infinity);
// 2. 先通过toString转成字符串,再塞到数组里,使用map遍历将字符串转成数字
arr.toString().split(',').map(item => +item);
// 3. reduce
const flatten = arr => arr.reduce((prev,next) => {
return Object.prototype.toString.call(next) === '[object Array]' ? prev.concat(flatten(next)) : prev.concat(next);
},[])
// 4. 使用apply
function flat(arr) {
let isDeep = arr.some(item => item instanceof Array)
if (!isDeep) return arr;
const res = Array.prototype.concat.apply([], arr)
return flat(res)
}