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());
  • 相关阅读:
    python爬虫简单代码爬取郭德纲单口相声
    WordPress 新版本中编辑器不好用, 使用原有编辑器
    hexo博客更新主题后上传Git操作
    Flask的Context(上下文)学习笔记
    Flask 中的 特殊装饰器before_request/after_request
    Flask 中的 CBV 与上传文件
    Flask 中的蓝图(BluePrint)
    【openresty】获取post请求数据FormInputNginxModule模块
    【随笔】Linux服务器备份相关
    【WMware】关于VMware服务器虚拟化管理之服务器容量扩充
  • 原文地址:https://www.cnblogs.com/xiewangfei123/p/13819134.html
Copyright © 2011-2022 走看看