• Web前端高频面试题解析(javascript篇)--- 每日十题(6)


    1.JS中清空数组的方法

    var arr=[1,2,3,4,5]

    1.splice

    影响原数组

    • 第一个参数:规定添加或者删除元素的位置;
    • 第二个参数:要删除元素的数量
    1. var arr = [1, 2, 3, 4, 5]
    2. arr.splice(0, arr.length)
    3. console.log(arr)//[]

    2.length

    数组的长度

    1. console.log(arr.length)//0
    2. arr.length = 0
    3. console.log(arr)//[]

    3.赋值为[]

    1. arr = []
    2. console.log(arr)//[]

    2.JS中如何实现继承

    B继承于A,A是父类,B是子类;继承可以让字类具有父类的各种属性和方法

    • 类:汽车
    • 属性:颜色,轮胎,品牌
    • 轿车:后备箱
    • 货车:大货箱

    1.实现方法:

    1.原型链的继承

    1. function Person () {
    2. this.name = 'Person'
    3. this.arr = [1, 2, 3]
    4. }
    5. // 通过实例化对象
    6. // console.log(new Person())
    7. function Child () {
    8. this.type = 'child'
    9. }
    10. Child.prototype = new Person()
    11. // 原型属性
    12. Child.prototype.age = 18
    13. // 实例化对象
    14. console.log(new Child().age)//拿age
    15. console.log(new Child())
    16. console.log(new Child().arr)//(3) [1, 2, 3]

    2.原型链继承的弊端:

    改变一个,也会影响另外一个。子类的实例对象使用的是同一个原型对象

    1. function Person () {
    2. this.name = 'Person'
    3. this.arr = [1, 2, 3]
    4. }
    5. function Child () {
    6. this.type = 'child'
    7. }
    8. Child.prototype = new Person()
    9. var c1 = new Child()
    10. var c2 = new Child()
    11. c1.arr.push(5)
    12. console.log(c1)
    13. console.log(c2);
    14. // 使用的同一个原型对象,改变一个,另外一个也会改变

     

     3.构造函数继承

    借用call,改变this指向

    1. function Person () {
    2. this.name = 'Person'
    3. this.arr = [1, 2, 3]
    4. }
    5. function Child () {
    6. Person.call(this)//this指向实例对象
    7. this.type = 'child'
    8. }
    9. console.log(new Child());

     4.解决了原型的弊端

    构造函数不能继承原型属性的方法和属性。

    继承方式只能继承父类的实例属性和方法,不能继承原型属性或者方法。 

    1. function Person () {
    2. this.name = 'Person'
    3. this.arr = [1, 2, 3]
    4. }
    5. // 构造函数不能继承原型属性的方法和属性
    6. Person.prototype.age = 18
    7. function Child () {
    8. Person.call(this)//this指向实例对象
    9. this.type = 'child'
    10. }
    11. console.log(new Child())
    12. var c1 = new Child()
    13. var c2 = new Child()
    14. c1.arr.push(5)
    15. console.log(c1)
    16. console.log(c2);

    5.组合继承

    最常用

    1. function Person () {
    2. this.name = 'Person'
    3. this.arr = [1, 2, 3]
    4. }
    5. // 定义一个age
    6. Person.prototype.age = 18
    7. function Child () {
    8. Person.call(this)
    9. this.type = 'child'
    10. }
    11. // 原型链的继承
    12. Child.prototype = new Person()
    13. // 需要自己指向自己
    14. Child.prototype.constructor = Child
    15. var c1 = new Child()//通过子类实例的
    16. var c2 = new Child()
    17. console.log(c1.constructor)// function Person () --> Child ()
    18. c1.arr.push(5)
    19. console.log(c1)
    20. console.log(c2);

    待学习文件:

     https://cloud.tencent.com/developer/article/1991885

  • 相关阅读:
    【密码学】RSA密码体制存在的问题
    从 0 到 1 ,手把手教你编写《消息队列》项目(Java实现) —— 介绍项目/ 需求分析
    【安卓学习之常见问题】自定义组件-刷新后跳到第一行
    【C++·峰顶计划】命名空间与Cin输入Cout输出操作
    C++并发编程
    apache VirtualHost Alias 配置
    git stash的使用方法
    神经网络算法处理器设计,神经网络是机器算法吗
    PHICOMM(斐讯)N1盒子 - Armbian5.77(Debian 9)配置自动连接WIFI无线网络
    项目架构:eslint 代码检测、提交代码审查
  • 原文地址:https://blog.csdn.net/m0_54088431/article/details/126329127