zoukankan      html  css  js  c++  java
  • ES6中 的类(class)

    程序中类:

            面向对象,类
                    属性:
                    方法:
            函数模拟:

            人:Person
                属性:name
                展示名字:showName

        Es5:
            function Person(){
                this.name = "aaa",
            }
            Person.prototype.showName=function(){}

            Object.assign( Person.prototype,{
                showName(){},
                showAge(){}
            })
        ES6:
            class :
                constructor(){}  <!-- 构造方法(函数),只要调用new,自动执行 -->

                1. class Person{
                    constructor(name,age){
                        <!-- 构造方法(函数),只要调用new,自动执行 -->
                        console.log("构造函数执行了");
                    }
                    showName(){}
                }
                let p = new Person("颉旺飞",18);

                2. 
                    const Person = class{}

                3. 
                    let a = "strive";
                    let b = "method";
                    class Person{
                        [a+b](){}            
                    }
            注意:
                1. ES6里面class没有提升功能,在ES5,用函数模拟可以,默认函数提升
                2. ES6中this比之前轻松多了
            

            矫正this:
                1. fn.call(this指向谁,arg1,arg2...)
                2. fn.apply(this指向谁,[arg1,arg2...])
                3. fn.bind()
            
            class里面取值函数(getter),存值函数(setter)

            静态方法:就是类身上的方法
                    static aaa(){}
                    父类.aaa();
         
            继承:extends
                // 父类
                class Person{
                    constructor(name) {
                        this.name = name
                    }
                    showName(){
                        return `名字为:${this.name}`
                    }
                }
                // 子类
                class Student extends Person {} <!-- extends 继承 -->
                // 调用
                let stu1 = new Student("");
                console.log(stu1.showName());
  • 相关阅读:
    vsftp
    数据类型
    第三篇:表相关操作
    第二篇:库相关操作
    第一篇: 初识数据库
    五 python并发编程之IO模型
    四 python并发编程之协程
    Python GIL
    三 python并发编程之多线程-重点
    三 python并发编程之多线程-理论
  • 原文地址:https://www.cnblogs.com/xiewangfei123/p/13819134.html
Copyright © 2011-2022 走看看