• 技术文章 >web前端 >前端问答

    ES6通过什么定义类

    青灯夜游青灯夜游2022-03-09 18:52:28原创127

    在ES6中,class(类)作为对象的模板被引入,可以通过“class”关键字来定义类。class的本质是function,它可以看作一个语法糖,让对象原型的写法更加清晰、更像面向对象编程的语法。

    本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。

    ES6 Class

    在ES6中,class(类)作为对象的模板被引入,可以通过“class”关键字来定义类。

    class 的本质是 function。

    基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。

    基础用法

    类定义

    类表达式可以为匿名或命名。

    // 匿名类
    let Example = class {
        constructor(a) {
            this.a = a;
        }
    }
    // 命名类
    let Example = class Example {
        constructor(a) {
            this.a = a;
        }
    }

    类声明

    class Example {
        constructor(a) {
            this.a = a;
        }
    }

    注意要点:不可重复声明。

    class Example{}
    class Example{}
    // Uncaught SyntaxError: Identifier 'Example' has already been 
    // declared
     
    let Example1 = class{}
    class Example{}
    // Uncaught SyntaxError: Identifier 'Example' has already been 
    // declared

    注意要点:

    类定义不会被提升,这意味着,必须在访问前对类进行定义,否则就会报错。

    类中方法不需要 function 关键字。

    方法间不能加分号。

    new Example(); 
    class Example {}

    类的主体

    属性

    prototype

    ES6 中,prototype 仍旧存在,虽然可以直接自类中定义方法,但是其实方法还是定义在 prototype 上的。 覆盖方法 / 初始化时添加方法

    Example.prototype={
        //methods
    }

    添加方法

    Object.assign(Example.prototype,{
        //methods
    })

    静态属性

    静态属性:class 本身的属性,即直接定义在类内部的属性( Class.propname ),不需要实例化。 ES6 中规定,Class 内部只有静态方法,没有静态属性。

    class Example {
    // 新提案
        static a = 2;
    }
    // 目前可行写法
    Example.b = 2;

    公共属性

    class Example{}
    Example.prototype.a = 2;

    实例属性

    实例属性:定义在实例对象( this )上的属性。

    class Example {
        a = 2;
        constructor () {
            console.log(this.a);
        }
    }

    name 属性

    返回跟在 class 后的类名(存在时)。

    let Example=class Exam {
        constructor(a) {
            this.a = a;
        }
    }
    console.log(Example.name); // Exam
     
    let Example=class {
        constructor(a) {
            this.a = a;
        }
    }
    console.log(Example.name); // Example

    方法

    constructor 方法

    constructor 方法是类的默认方法,创建类的实例化对象时被调用。

    class Example{
        constructor(){
          console.log('我是constructor');
        }
    }
    new Example(); // 我是constructor

    返回对象

    class Test {
        constructor(){
            // 默认返回实例对象 this
        }
    }
    console.log(new Test() instanceof Test); // true
     
    class Example {
        constructor(){
            // 指定返回对象
            return new Test();
        }
    }
    console.log(new Example() instanceof Example); // false

    静态方法

    class Example{
        static sum(a, b) {
            console.log(a+b);
        }
    }
    Example.sum(1, 2); // 3

    原型方法

    class Example {
        sum(a, b) {
            console.log(a + b);
        }
    }
    let exam = new Example();
    exam.sum(1, 2); // 3

    实例方法

    class Example {
        constructor() {
            this.sum = (a, b) => {
                console.log(a + b);
            }
        }
    }

    类的实例化

    new

    class 的实例化必须通过 new 关键字。

    class Example {}
     
    let exam1 = Example(); 
    // Class constructor Example cannot be invoked without 'new'

    实例化对象

    共享原型对象

    class Example {
        constructor(a, b) {
            this.a = a;
            this.b = b;
            console.log('Example');
        }
        sum() {
            return this.a + this.b;
        }
    }
    let exam1 = new Example(2, 1);
    let exam2 = new Example(3, 1);
     
    // __proto__ 已废弃,不建议使用
    // console.log(exam1.__proto__ == exam2.__proto__); 
     
    console.log(Object.getPrototypeOf(exam1) === Object.getPrototypeOf(exam2));// true
     
    Object.getPrototypeOf(exam1).sub = function() {
        return this.a - this.b;
    }
    console.log(exam1.sub()); // 1
    console.log(exam2.sub()); // 2

    【相关推荐:javascript视频教程web前端

    以上就是ES6通过什么定义类的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:ES6 Class
    上一篇:es6 set是什么意思 下一篇:es6中map和foreach有什么区别
    Web大前端开发直播班

    相关文章推荐

    • es6怎么判断数组是否含有某个子元素• es6中怎么将数组转为对象• es6怎么判断变量是不是数组• es6 symbol属于基本类型吗• es6怎么判断是不是数字

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网