美文网首页
JavaScript ES6 类的静态方法、属性和实例方法、属性

JavaScript ES6 类的静态方法、属性和实例方法、属性

作者: 注定暂时漂泊xxx | 来源:发表于2019-06-05 20:40 被阅读0次
    • 类相当于实例的原型,ES6类的声明中可以定义实例方法、实例属性和静态方法。
    • ES6 明确规定, Class 内部只有静态方法, 没有静态属性
    • ES7 有一个静态属性的提案, 目前 Babel 转码器支持。该提案允许在Class声明内部定义静态属性(属性声明前加上static关键字)

    实例方法(属性)、静态方法(属性)的区别

    实例方法、实例属性

    声明方式

    • 实例方法定义在类的class声明里
    • 实例属性定义在类的constructor方法里 (ES6)
    • ES7中类的实例属性可以用等式, 写入类的定义之中。

    代码示例:

    //ES6
    class ReactCounter extends React.Component {  
        constructor(props) {  
            super(props);  
            
            this.state = {  
                count: 0  
            };  
            
            this.myInsProps = 456;   //实例属性声明
        }  
    }  
    let myCounter = new ReactCounter();
    myCounter.myInsProps;   //456
    
    
    
    //ES7
    class MyClass {  
        myProp = 42;    //实例属性声明
        constructor() {  
            console.log(this.myProp); // 42  
        }  
    }  
    
    let myObj = new MyClass ();
    myObj.myProp;     //42
    
    
    //ES7  
    //为了可读性的目的, 对于那些在constructor里面已经定义的实例属性, 新写法允许直接列出。
    class ReactCounter extends React.Component {  
        constructor(props) {  
            super(props);  
            this.state = {  
                count: 0  
            };  
        }  
        state;  
    }
    

    使用方式

    • 实例变量直接调用

    静态方法、静态属性

    声明方式

    • 静态方法:可以在Class内部声明,声明方式为在前面加上 static 关键字
    • 静态属性(ES6): 不能在Class内部声明,声明方式为通过直接点方法赋值
    • 静态属性(ES7):同ES6的静态方法声明方式,可以在Class内部声明,声明方式为在属性声明前加上static关键字

    代码示例:

    //ES6:
    class Foo {}
    Foo.prop = 1;    //静态属性(类的属性)
        
    //ES7:
    class Foo {  
        static prop = 1;    //静态属性
    }  
    
    class MyClass {
        static myStaticProp = 42;
        constructor() {
            console.log(MyClass.myProp); // 42
        }
    }
    

    老写法的静态属性定义在类的外部。 整个类生成以后, 再生成静态属性。 这样让人很容易忽略这个静态属性, 也不符合相关代码应该放在一起的代码组织原则。 另外, 新写法是显式声明( declarative), 而不是赋值处理, 语义更好。

    使用方式

    • 不会被实例继承
    • 只能直接通过类来调用
    • 可以被子类继承
    • 可以从super对象上调用
    class Foo {  
        static classMethod() {      //-> 静态方法在Class内部通过static关键字声明
            return 'hello';  
        }
        static classProp = 20;   //静态属性(ES7写法)
    }
    Foo.otherProps = 666;     //静态属性(ES6写法)
    
    Foo.classMethod() // 'hello'     -> 只能直接通过类来调用
    var foo = new Foo();  
    foo.classMethod();  // TypeError: foo.classMethod is not a function  -> 不会被实例继承
    
    console.log(Foo.classProp);  //20
    console.log(Foo.otherProps);  //666
    
    
    
    class Bar extends Foo {}  
    Bar.classMethod(); // 'hello'   //->可以被子类继承
    console.log(Bar.classProp);   //20  -> 静态属性可以被子类继承(ES7)
    console.log(Bar.otherProps);  //666  -> 静态属性可以被子类继承(ES6)
    
    
    class Bar extends Foo {  
        static classMethod() {  
            return super.classMethod() + ', too';    //->可以从super对象上调用
        }  
    }  
    Bar.classMethod();     // "hello, too"
    
    

    参考资料:https://blog.csdn.net/qq_30100043/article/details/53542966

    相关文章

      网友评论

          本文标题:JavaScript ES6 类的静态方法、属性和实例方法、属性

          本文链接:https://www.haomeiwen.com/subject/gjtexctx.html