爱奇艺 2020年2月13日
-
自我介绍
-
==
与===
-
call和apply
-
几种继承方式及优缺点
原型链继承: 将父类的私有属性和公有方法都作为自己的公有属性和方法,但无法实现多继承,无法向父类构造函数传参,父类所有属性将被共享。
构造函数继承: 解决了原型链继承中子类实例共享父类引用属性的问题,可以向父类传递参数,可以多继承(call多个父类对象),但只能继承父类的实例属性和方法,不能继承原型属性和方法,
组合继承: 通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用。需要修复构造函数指向,会调用两次构造函数。 优化:通过父类原型和子类原型指向同一对象Fish.prototype = Animal.prototype
,子类可以继承到父类的公有方法当做自己的公有方法,而且不会初始化两次实例方法/属性,但是没办法辨别是实例是子类还是父类创造的使用instanceof Fish/Animal
都返回true。
寄生继承:在组合继承的基础上进行优化,Fish.prototype = Object.create(Animal.prototype)
,同样的,需要修复构造函数指向。手动实现一个object.create Object.create = function (o) { var F = function () {}; F.prototype = o; return new F(); };
-
React组件传值和数据共享
-
redux原理
-
数组排序(时间复杂度/空间复杂度):选择排序,时间复杂度O(n^2),空间复杂度O(1)
const arr = [2, 1, 4, 3, 6, 5, 7, 0]; sort(arr); // [0, 1, 2, 3, 4, 5, 6, 7] function sort(arr) { for (var i = 0; i < arr.length; i++) { for (var j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { var temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } return arr }
-
函数执行结果(变体,如果getFullname改为箭头函数,则会是什么结果。箭头函数与普通函数的区别)
var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: { fullname: 'Aurelio De Rosa', getFullname: function(){ return this.fullname; } } } console.log(obj.prop.getFullname()); // 'Aurelio De Rosa' var test = obj.prop.getFullname; console.log(test()); // 'John Doe';
-
写一个继承Animal的class/function,并在Animal的基础上有自己的bark
function Animal(name, energy){ this.name = name; this.energy = energy; } Animal.prototype.eat = function(amount){ console.log(`${this.name} is eating`); this.energy += amount; } Animal.prototype.sleep = function(length){ console.log(`${this.name} is sleeping`); this.energy -= length; } A: function Fish(name, energy){ Animal.call(this, name, energy); } Fish.prototype = new Animal(); Fish.prototype.bark = function(){ console.log('this is bark'); }