ES6_类_note

43 篇文章 1 订阅

程序中的类:

  • 注意:

1.es6中class没有哦提升功能,在es5中,函数模拟可以,默认函数提升

2.this

矫正this:

1.fn.call(this指向谁,args1,args2…)

2.fn.apply(this指向,[args1,args2…])

3.fn.bind()

class中取值函数(getter),存执函数(setter)

静态方法:类身上的方法,如:

<script >
    class Person {
        constructor() {
        }
        showName() {
            return '这是showName方法';
        }
        // 定义静态方法
        static aaa() {
            return '这是静态方法';
        }
    }
    let p1 = new Person();
    console.log(p1.showName());
    // 调用静态方法
    console.log(Person.aaa());
</script>
</script>

父类

子类

继承:

之前使用继承(很累):

<script >
    //  父类
    function Person(name) {
        this.name = name;
    }

    Person.prototype.showName = function () {
        return `名字是:${this.name}`;
    };

    // 子类
    function Student(name, skill) {
        Person.call(this, name); // 继承属性
        this.skill = skill;
    }

    Student.prototype = new Person(); // 继承方法

    // 调用
    let stu1 = new Student("kirin", "逃学");
    console.log(stu1.showName());
</script>

现在:

<script >
    class Person {
        constructor(name) {
            this.name = name;
        }

        showName() {
            console.log("父类的showName");
            return `名字为: ${this.name}`;
        }
    }

    class Student extends Person {
        // 子类继承父类必须在其中写 super()
        // constructor:构造函数
        /*constructor(args) {
            super(args);
        }*/
        constructor(name, skill) {
            super(name);
            this.skill = skill;
        }
        // 如果子类中有覆盖了父类的方法,则子类方法中必须写super()
        showName() {
            super.showName(); // 父类的方法执行,后面子类的代码还会继续执行
            console.log("子类里的showName");
        }

        showSkill() {
            return `技能为${this.skill}`;
        }
    }

    let stu1 = new Student("kirin", "逃学");
    // console.log(stu1.showName());
    // console.log(stu1.showSkill());
    stu1.showName();
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无名之辈无名之辈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值