• 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());
  • 相关阅读:
    C#Task和async/await使用
    jQuery实现C#CheckBoxList模糊搜索
    [转载].NET ASP.NET 中web窗体(.aspx)利用ajax实现局部刷新
    MD5加密方法HashPasswordForStoringInConfigFile(string,string)过时问题处理方法
    .NET WebFrom跨时区项目时间问题处理方法
    C# 中的Async 和 Await 的用法详解
    cron表达式
    ASP.NET MVC实现单用户登录
    一个简单便捷的树形显示Ztree
    身份证代码
  • 原文地址:https://www.cnblogs.com/xiewangfei123/p/13819134.html
Copyright © 2020-2023  润新知