zoukankan      html  css  js  c++  java
  • ES6知识点整理之----Class----语法

    ES6 引入了 Class(类)这个概念,作为对象的模板。通过class关键字,可以定义类。

    //定义类
    class Point {
      constructor(x, y) {
        this.x = x;
        this.y = y;
      }
    
      toString() {
        return '(' + this.x + ', ' + this.y + ')';
      }
    }

    ES6 的类,完全可以看作构造函数的另一种写法。

    class Point {
      // ...
    }
    
    typeof Point // "function"
    Point === Point.prototype.constructor // true

    使用的时候,也是直接对类使用new命令,跟构造函数的用法完全一致。

    class Bar {
      doStuff() {
        console.log('stuff');
      }
    }
    
    var b = new Bar();
    b.doStuff() // "stuff"

    构造函数的prototype属性,在 ES6 的“类”上面继续存在。事实上,类的所有方法都定义在类的prototype属性上面。

    类的内部所有定义的方法,都是不可枚举的(non-enumerable)。

    类的属性名,可以采用表达式。

    let methodName = 'getArea';
    
    class Square {
      constructor(length) {
        // ...
      }
    
      [methodName]() {
        // ...
      }
    }

    类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式。ES6 实际上把整个语言升级到了严格模式。

    constructor 方法

    类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。

    class Point {
    }
    
    // 等同于
    class Point {
      constructor() {}
    }

    constructor方法默认返回实例对象(即this),但是也可以指定返回另外一个对象。

    class Foo {
      constructor() {
        return Object.create(null);
      }
    }
    
    new Foo() instanceof Foo
    // false

    类必须使用new调用,否则会报错。这是它跟普通构造函数的一个主要区别,后者不用new也可以执行。

    class Foo {
      constructor() {
        return Object.create(null);
      }
    }
    
    Foo()
    // TypeError: Class constructor Foo cannot be invoked without 'new'

    类的实例对象

    生成类的实例对象的写法,与 ES5 完全一样,也是使用new命令。

    与 ES5 一样,实例的属性除非显式定义在其本身(即定义在this对象上),否则都是定义在原型上(即定义在class上)。

    //定义类
    class Point {
    
      constructor(x, y) {
        this.x = x;
        this.y = y;
      }
    
      toString() {
        return '(' + this.x + ', ' + this.y + ')';
      }
    
    }
    
    var point = new Point(2, 3);
    
    point.toString() // (2, 3)
    
    point.hasOwnProperty('x') // true
    point.hasOwnProperty('y') // true
    point.hasOwnProperty('toString') // false
    point.__proto__.hasOwnProperty('toString') // true

    与 ES5 一样,类的所有实例共享一个原型对象。

    var p1 = new Point(2,3);
    var p2 = new Point(3,2);
    
    p1.__proto__ === p2.__proto__
    //true

    可以通过实例的__proto__属性为“类”添加方法(不推荐)

    var p1 = new Point(2,3);
    var p2 = new Point(3,2);
    
    p1.__proto__.printName = function () { return 'Oops' };
    
    p1.printName() // "Oops"
    p2.printName() // "Oops"
    
    var p3 = new Point(4,2);
    p3.printName() // "Oops"

    Class 表达式

    与函数一样,类也可以使用表达式的形式定义。

    const MyClass = class Me {
      getClassName() {
        return Me.name;
      }
    };
    
    //需要注意的是,这个类的名字是MyClass而不是Me,Me只在 Class 的内部代码可用,指代当前类。

    如果类的内部没用到的话,可以省略Me,也就是可以写成下面的形式。

    const MyClass = class { /* ... */ };

    采用 Class 表达式,可以写出立即执行的 Class。

    let person = new class {
      constructor(name) {
        this.name = name;
      }
    
      sayName() {
        console.log(this.name);
      }
    }('张三');
    
    person.sayName(); // "张三"

    不存在变量提升

    类不存在变量提升(hoist),这一点与 ES5 完全不同。这种规定的原因与继承有关,必须保证子类在父类之后定义。

    new Foo(); // ReferenceError
    class Foo {}

    私有方法和私有属性

    ES6 不提供,只能通过变通方法模拟实现

    • 在命名上加以区别:
    class Widget {
    
      // 公有方法
      foo (baz) {
        this._bar(baz);
      }
    
      // 私有方法
      _bar(baz) {
        return this.snaf = baz;
      }
    
      // ...
    }
    • 将私有方法移出模块,因为模块内部的所有方法都是对外可见的。
    class Widget {
      foo (baz) {
        bar.call(this, baz);
      }
    
      // ...
    }
    
    function bar(baz) {
      return this.snaf = baz;
    }
    • 利用Symbol值的唯一性,将私有方法的名字命名为一个Symbol
    const bar = Symbol('bar');
    const snaf = Symbol('snaf');
    
    export default class myClass{
    
      // 公有方法
      foo(baz) {
        this[bar](baz);
      }
    
      // 私有方法
      [bar](baz) {
        return this[snaf] = baz;
      }
    
      // ...
    };

    私有属性的提案

    有一个提案,为class加了私有属性。方法是在属性名之前,使用#表示。使用时必须带有#一起使用。

    class Point {
      #x;
    
      constructor(x = 0) {
        #x = +x; // 写成 this.#x 亦可
      }
    
      get x() { return #x }
      set x(value) { #x = +value }
    }

    这种写法不仅可以写私有属性,还可以用来写私有方法。

    class Foo {
      #a;
      #b;
      #sum() { return #a + #b; }
      printSum() { console.log(#sum()); }
      constructor(a, b) { #a = a; #b = b; }
    }

    私有属性也可以设置 getter 和 setter 方法。

    class Counter {
      #xValue = 0;
    
      get #x() { return #xValue; }
      set #x(value) {
        this.#xValue = value;
      }
    
      constructor() {
        super();
        // ...
      }
    }

    私有属性不限于从this引用,类的实例也可以引用私有属性。但是,直接从实例上引用私有属性是不可以的,只能在类的定义中引用。

    class Foo {
      #privateValue = 42;
      static getPrivateValue(foo) {
        return foo.#privateValue;
      }
    }
    
    Foo.getPrivateValue(new Foo()); // 42

    this 的指向

    类的方法内部如果含有this,它默认指向类的实例。

    name 属性

    返回紧跟在class关键字后面的类名。

    class Point {}
    Point.name // "Point"

    Class 的取值函数(getter)和存值函数(setter)

    在“类”的内部可以使用getset关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。

    class MyClass {
      constructor() {
        // ...
      }
      get prop() {
        return 'getter';
      }
      set prop(value) {
        console.log('setter: '+value);
      }
    }
    
    let inst = new MyClass();
    
    inst.prop = 123;
    // setter: 123
    
    inst.prop
    // 'getter'

    存值函数和取值函数是设置在属性的 Descriptor 对象上的。

    class CustomHTMLElement {
      constructor(element) {
        this.element = element;
      }
    
      get html() {
        return this.element.innerHTML;
      }
    
      set html(value) {
        this.element.innerHTML = value;
      }
    }
    
    var descriptor = Object.getOwnPropertyDescriptor(
      CustomHTMLElement.prototype, "html"
    );
    
    "get" in descriptor  // true
    "set" in descriptor  // true
  • 相关阅读:
    Java读取resource文件/路径的几种方式
    log4j:WARN Please initialize the log4j system properly解决办法
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    log4j.properties配置详解与实例-全部测试通过[转]
    testNG中dataprovider使用的两种方式
    远程仓库获取最新代码合并到本地分支
    git 冲突解决办法
    【转】JsonPath教程
    selenium及webdriver的原理【转】
    [转]Redis 数据结构简介
  • 原文地址:https://www.cnblogs.com/adhehe/p/9686207.html
Copyright © 2011-2022 走看看