JavaScript 进阶第九章(原型链继承)

面向对象的特性 封装 继承

子类strudent 继承了父类 Person的属性

  // 父类
    function Person(name, height) {
      this.name = name;
      this.height = height;
    }

    Person.prototype.say = function () {
      console.log(this.name);
      console.log(this.height);
    }

    // 子类
    function Student(grade, name, height) {
      // 借用了父类的构造函数,完成对自己的赋值
      Person.call(this, name, height)
      this.grade = grade;
    }

    // 赋值了父类原型上的所有的 属性和方法
    Student.prototype = Person.prototype;
    // 修改之类的指向
    Student.prototype.constructor = Student;

    // 创建子类的实例
    const stu = new Student("两年", "海海呐", 180);
    stu.say();

函数参数默认值

定义函数的同时,可以给形参一个默认值

    // 定义函数的同时,可以给形参一个默认值
    function show(msg = "你好呀") {
      console.log(msg);
    }

    show(); // 你好呀
    show("你好,再见"); // 你好,再见

对象简写

    const name = "李白";
    const skill = "大河之剑??";
    const say = function () { }
    const obj = {
      name, skill, say
    }
    console.log(obj);// {name:"
李白",skill:"大河之剑??",say:function(){}}

解构

提供更加方便获取数组中元素或者对象中属性的写法

获取数组中的元素

    const [a, b, c, d] = [1, 2, 3, 4];
      console.log(a, b, c, d); // 1,2,3,4

元素交互顺序

     let a = 1111;
     let b = 2222;
     [b, a] = [a, b];
     console.log(a, b); // 2222 1111 

获取对象中的属性

 const obj = {
        name: "李白",
        skill: "大河之剑???",
        say() { }
      }
      const { name, skill,say } = obj;
      console.log(name, skill,say); // 李白 大河之剑??? function(){}

拓展运算符 || 剩余运算符

通过  ... 符号来获取剩下的参数

函数内获取

    function show(a, ...all) { 
      console.log(a);
      console.log(all);
    }


    show(1);// 1 []
    show(1, 2, 3); // 1 [2,3]

数组内获取

     const [a, ...rest] = [1, 2, 3, 4, 5];
    console.log(a); // 1
    console.log(rest);// [2, 3, 4, 5]

对象内获取

   const obj={
      name:"海海",
      age:"18",
      say(){}
    }

    const {name,...others}=obj;
    console.log(name); // 海海
    console.log(others); // {age: "18", say: ƒ}

 上一章:JavaScript 进阶第八章(闭包)

 下一章:JavaScript 进阶第十章(递归)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海海呐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值