前端面试必问:javascript原型和继承

那么依据这个思想来实现一下instanceof吧,一定会印象更加深刻

function myInstanceof2(left, right){

if(left === null || left === undefined){

return false

}

if(right.prototype === left.proto) {

return true

}

left = left.proto

return myInstanceof2(left, right)

}

console.log(myInstanceof2([], Array))

4 new 模拟实现(简要版)

new的过程发生了什么?

  1. 生成空对象

  2. 这个空对象的__proto__赋值为构造函数的prototype

  3. 绑定this指向

  4. 返回这个对象

// 构造函数

function M(name){

this.name = name

}

// 原生new

var obj = new M(‘123’)

// 模拟实现

function create() {

// 生成空对象

let obj = {}

// 拿到传进来参数的第一项,并改变参数类数组

let Con = [].shift.call(arguments)

// 对空对象的原型指向赋值

obj.proto = Con.prototype

// 绑定this

//(对应下面使用来说明:Con是参数第一项M,

// arguments是参数[‘123’],

// 就是 M方法执行,参数是’123’,执行这个函数的this是obj)

let result = Con.apply(obj, arguments)

return result instanceof Object ? result : obj

}

var testObj = create(M, ‘123’)

console.log(‘testObj’, testObj)

5 继承的实现(逐步实现)

一步一步来,从简到繁,更能直观发现继承的原理与缺点

  1. 构造方法方式 核心 Parent1.call(this)

// 构造方法方式

function Parent1(){

this.name = ‘Parent1’

}

Parent1.prototype.say = function () {

alert(‘say’)

}

function Child1(){

Parent1.call(this)

this.type = ‘type’

}

var c1 = new Child1()

c1.say() //报错

缺点: 只能继承父类构造函数内部属性,无法继承父类构造函数原型对象上属性

思考: 为什么 call 实现了继承,call本质是什么?

  1. 只借助原型继承 核心 Child2.prototype = new Parent2()

// 原型

function Parent2(){

this.name = ‘Parent2’

this.arr = [1,2]

}

Parent2.prototype.say = function () {

alert(‘say’)

}

function Child2(){

// Parent2.call(this)

this.type = ‘type’

}

Child2.prototype = new Parent2()

var c21 = new Child2()

var c22 = new Child2()

c21.say()

c21.arr.push(‘9’)

console.log('c21.arr : ', c21.arr)

console.log('c22.arr : ', c22.arr)

缺点: c21.arr 与c22.arr对应的是同一个引用

思考:为什么这么写是同一个引用?

  1. 组合继承1

把上面两个继承方式的优点合并起来,缺点都抛弃掉

function Parent3(){

this.name = ‘Parent3’

this.arr = [1,2]

}

Parent3.prototype.say = function () {

alert(‘say’)

}

function Child3(){

Parent3.call(this)

this.type = ‘type’

}

Child3.prototype = new Parent3()

var c31 = new Child3()

var c32 = new Child3()

c31.say()

c31.arr.push(‘9’)

console.log('c31.arr : ', c31.arr)

console.log('c31.arr : ', c32.arr)

思考: 这么写就没有问题了吗?

答 : 生成一个实例要执行 Parent3.call(this) , new Child3(),也就是Parent3执行了两遍。

  1. 组合继承2

改变上例子 的

Child3.prototype = new Parent3()

Child3.prototype = Parent3.prototype

缺点 : 很明显,无法定义子类构造函数原型私有的方法

  1. 组合继承优化3 再次改变上例子 的

Child3.prototype = Parent3.prototype

Child3.prototype = Object.create(Parent3.prototype)

问题就都解决了。 因为Object.create的原理是:生成一个对象,这个对象的__proto__, 指向所传的参数。

思考 :是否还有疏漏?一时想不起来的话,可以看下这几个结果

console.log(c31 instanceof Child3) // true

console.log(c31 instanceof Parent3) // true

console.log(c31.constructor === Child3) // false

console.log(c31.constructor === Parent3) // true

所以回想起文章开头所说的那几个需要牢记的点,就需要重新赋值一下子类构造函数的constructor: Child3.prototype.constructor = Child3,完整版如下

function Parent3(){

this.name = ‘Parent3’

this.arr = [1,2]

}

Parent3.prototype.say = function () {

alert(‘say’)

}

function Child3(){

Parent3.call(this)

this.type = ‘type’

}

Child3.prototype = Object.create(Parent3.prototype)

Child3.prototype.constructor = Child3

var c31 = new Child3()

var c32 = new Child3()

c31.say()

c31.arr.push(‘9’)

console.log('c31.arr : ', c31.arr)

console.log('c31.arr : ', c32.arr)

数据结构与算法

这一块在笔试、面试的代码题中考核较多,其中常考的数据结构主要有:数组、链表、队列、栈、Set、Map、哈希表等,不同数据结构有不同的方法以及储存原理,这些算是技术岗的必备知识。算法部分主要分为两大块,排序算法与一些其他算法题

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

排序算法根据考频高低主要有:快速排序、归并排序、堆排序、冒泡排序、插入排序、选择排序、希尔排序、桶排序、基数排序、Timsort这十种,这类考核点要么是算法的时间、空间复杂度、稳定度,要么是直接手写代码,故在理解算法原理的同时,对JS语言版的排序算法代码也要加强记忆。

  • 二叉树层序遍历
  • B 树的特性,B 树和 B+树的区别
  • 尾递归
  • 如何写一个大数阶乘?递归的方法会出现什么问题?
  • 把多维数组变成一维数组的方法
  • 知道的排序算法 说一下冒泡快排的原理
  • Heap 排序方法的原理?复杂度?
  • 几种常见的排序算法,手写
  • 数组的去重,尽可能写出多个方法
  • 如果有一个大的数组,都是整型,怎么找出最大的前 10 个数
  • 知道数据结构里面的常见的数据结构
  • 找出数组中第 k 大的数组出现多少次,比如数组【1,2, 4,4,3,5】第二大的数字是 4,出现两次,所以返回 2
  • 合并两个有序数组
  • 给一个数,去一个已经排好序的数组中寻找这个数的位 置(通过快速查找,二分查找)

、桶排序、基数排序、Timsort这十种,这类考核点要么是算法的时间、空间复杂度、稳定度,要么是直接手写代码,故在理解算法原理的同时,对JS语言版的排序算法代码也要加强记忆。

  • 二叉树层序遍历
  • B 树的特性,B 树和 B+树的区别
  • 尾递归
  • 如何写一个大数阶乘?递归的方法会出现什么问题?
  • 把多维数组变成一维数组的方法
  • 知道的排序算法 说一下冒泡快排的原理
  • Heap 排序方法的原理?复杂度?
  • 几种常见的排序算法,手写
  • 数组的去重,尽可能写出多个方法
  • 如果有一个大的数组,都是整型,怎么找出最大的前 10 个数
  • 知道数据结构里面的常见的数据结构
  • 找出数组中第 k 大的数组出现多少次,比如数组【1,2, 4,4,3,5】第二大的数字是 4,出现两次,所以返回 2
  • 合并两个有序数组
  • 给一个数,去一个已经排好序的数组中寻找这个数的位 置(通过快速查找,二分查找)

[外链图片转存中…(img-CDNPoK9G-1714630464382)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值