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());
  • 相关阅读:
    css3 box-sizing盒模型
    数字递增组件
    设置视频水平垂直居中显示在页面上
    修改placeholder样式,兼容多个浏览器
    一款还不错的日期插件layDate
    vue-cli打包后出现 “Uncaught SyntaxError: Unexpected token <”这个错
    详谈C++虚函数表那回事(一般继承关系)
    C++多态的实现及原理详细解析
    位运算求两个数的平均值
    网页设计入门<一>
  • 原文地址:https://www.cnblogs.com/xiewangfei123/p/13819134.html
Copyright © 2011-2022 走看看