zoukankan      html  css  js  c++  java
  • class 关键字

    class Ninja 表示创建一个名为Ninja的函数。
    constructor(...)指明Ninja函数的签名和函数体内容。

    class Ninja{
        constructor(name){
            this.name=name;
        }
        swingSword(){
            return true;
        }
    }
    const ninja=new Ninja("ml");

    等价于:

        function Ninja(name){
            this.name=name;
        }
        Ninja.prototype.swingSword=function(){
            return true;
        };
        const ninja=new Ninja("ml");

    使用extends关键字 实现继承。

     1     class Person{
     2         constructor(name){
     3             this.name=name;
     4         }
     5         dance(){
     6             return true;
     7         }
     8     }
     9     class Ninja extends Person{
    10         constructor(name,weapon){
    11             super(name);
    12             this.weapon=weapon;
    13         }
    14         wieldWeapon(){
    15             return true;
    16         }
    17     }
    18     var person=new Person("Bob");

    super 关键字用来调用父类的构造函数。

    使用这样的继承方法,不用担心原型或覆盖属性的副作用。

    因为构造器还在对象体内,只是借用方法和构造函数。

    静态方法:

        class Person{
            constructor(name){
                this.name=name;
            }
            dance(){
                return true;
            }
            static message(){
                returm true;
            }
        }

    static 定义的方法只能通过函数来调用  不能通过实例来访问。

    等价于:

    1     function Person(){
    2         this.name=name;
    3         this.dance=function(){
    4           return true;  
    5         };
    6     }
    7     Person.message=function(){
    8         return true;
    9     };
  • 相关阅读:
    一些$LCT$的瓜皮题目
    写点东西(关于背包问题)
    字符串算法总结
    常系数齐次线性递推
    原根算法与剩余定理
    问题集
    常用链接
    回形针PaperClip
    6.824拾遗
    杂项
  • 原文地址:https://www.cnblogs.com/alaner/p/9547264.html
Copyright © 2011-2022 走看看