前端面试题整理三

2020-03-16  本文已影响0人  忘川蒿里丶
1、继承的几种方式及优缺点?
function Parent () {
    this.name = 'kevin';
}

Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

console.log(child1.getName()) // kevin

缺点:1.引用类型的属性被所有实例共享
2.在创建 Child 的实例时,不能向Parent传参

function Parent () {
    this.names = ['kevin', 'daisy'];
}

function Child () {
    Parent.call(this);
}

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy"]

优点:1.避免了引用类型的属性被所有实例共享
2.可以在 Child 中向 Parent 传参
缺点:方法都在构造函数中定义,每次创建实例都会创建一遍方法。

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {

    Parent.call(this, name);
    
    this.age = age;

}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

child1.colors.push('black');

console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');

console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

优点:融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。

function createObj(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。
缺点:包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。

2、HTML5新增标签和使用

语义化的好处

布局相关的常用标签

<header>定义一个页面或是区域的头部</header>
<div>
        <section>定义一个区域</section>
        <aside>定义页面内容的侧边框部分</aside>
</div>
<footer>定义一个页面或是区域的底部</footer>
3、内存泄露

定义:应用程序不再需要占用内存的时候,由于某些原因,内存没有被操作系统或可用内存池回收
常见内存泄露的原因

4、ES6Object新增方法
let obj = {name:"winne",age:22};
let objKeys = Object.keys(obj);
let objValues = Object.values(obj);
let objItem = Object.entries(obj);

console.log(objKeys);   //["name","age"]
console.log(objValues); //["winne",22]
console.log(objItem);   //[["name","winne"],["age",22]]
5、ES6数组的map方法
 var arr = [1,2,3,4,5]
        var doubleNumber = arr.map(function(num){
            return num*2
        })    
     console.log(doubleNumber) //[2,4,6,8,10] 
var arr = [
            {
                name:'Jack',age:'16',sex:'男'     
            },{
                name:'Jerry',age:'18',sex:'男'    
            },{
                name:'Bailey',age:'14',sex:'女'    
            }
        ]
 var names = arr.map(function(name){
            return name.name;
        })
 console.log(names) //  ["Jack", "Jerry", "Bailey"]
6、ES6新增数组方法
// String
Array.from('abc'); // ["a", "b", "c"]
// Set
Array.from(new Set(['abc', 'def'])); // ["abc", "def"]
// Map
Array.from(new Map([[1, 'abc'], [2, 'def']])); // [[1, 'abc'], [2, 'def']]

2、Array.of

Array.of() // []
Array.of(undefined) // [undefined]
Array.of(3) // [3]
Array.of(3, 4, 5) // [3, 4, 5]
['a','b','c','d','e','f','g'].copyWithin(0, 3) //  ["d", "e", "f", "g", "e", "f", "g"]
['a','b','c','d','e','f','g'].copyWithin(0, 3, 4) //  ["d", "b", "c", "d", "e", "f", "g"]

2、fill
使用给定值,填充一个数组。

[1,2,3,4,5].fill('a');
// ["a", "a", "a", "a", "a"]

new Array(3).fill(12)
// [12, 12, 12]
['a','b','c','d'].includes('c',2)  //true
['a','b','c','d'].includes('c',3)  //false

3、entries、keys、values
用法类似对象

[1, 2, [3, 4]].flat()
// [1, 2, 3, 4]

2、flatMap
方法对原数组的每个成员执行一个函数(相当于执行 Array.prototype.map),然后对返回值组成的数组执行 flat() 方法。该方法返回一个新数组,不改变原数组。

[2, 3, 4].flatMap((x) => [x, x * 2])
// [2, 4, 3, 6, 4, 8]
// 相当于 [[2, 4], [3, 6], [4, 8]].flat()
上一篇 下一篇

猜你喜欢

热点阅读