zoukankan      html  css  js  c++  java
  • ES6类与模块

    class Animal {
        // 构造方法,实例化的时候会被调用,如果不指定,那么会有一个不带参数的默认构造函数
        constructor(name, color) {
            this.name = name;
            this.color = color;
        }
    
        // toString 是原型对象上的属性
        toString() {
            console.log('name: ' + this.name + ',color: ' + this.color);
        }
    }
    
    var animal = new Animal('dog', 'white');
    animal.toString(); // name: dog,color: white
    
    console.log(animal.hasOwnProperty('name')); // true
    console.log(animal.hasOwnProperty('toString')); //false
    console.log(animal.__proto__.hasOwnProperty('toString')); //true
    
    class Cat extends Animal {
        constructor(action) {
            // 子类必须要在constructor中指定super方法,否则在新建实例的时候会报错
            super('cat', 'white');
            this.action = action;
        }
        toString() {
            super.toString();
        }
    }
    
    var cat = new Cat('catch');
    cat.toString();
    console.log(cat instanceof Cat); // true
    console.log(cat instanceof Animal); // true

    类的 prototype 属性和 __proto__ 属性

    class Animal {
        // 构造方法,实例化的时候会被调用,如果不指定,那么会有一个不带参数的默认构造函数
        constructor(name, color) {
            this.name = name;
            this.color = color;
        }
    
        // toString 是原型对象上的属性
        toString() {
            console.log('name: ' + this.name + ',color: ' + this.color);
        }
    }
    
    
    class Cat extends Animal {
        constructor(action) {
            // 子类必须要在constructor中指定super方法,否则在新建实例的时候会报错
            super('cat', 'white');
            this.action = action;
        }
        toString() {
            super.toString();
        }
    }
    
    var cat = new Cat('catch');
    console.log(Cat.__proto__ === Animal); // true
    console.log(Cat.prototype.__proto__ == Animal.prototype); // true
    class Cat {}
    
    console.log(Cat.__proto__ === Function.prototype); // true
    console.log(Cat.prototype.__proto__ === Object.prototype); // true
  • 相关阅读:
    手把手教你如何安装Pycharm
    虚拟机的安装
    将BUG管理工具(禅道)部署到服务器(测试服务器、云服务器)
    XMind入门教程
    测试流程中的问题
    MySQL安装
    SDK?JDK?JDK 下载、安装、配置图文教程
    tomcat是什么?Tomcat 下载、安装、配置图文教程
    转:如何准备性能测试数据
    转:性能测试用例设计策略
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/6128130.html
Copyright © 2011-2022 走看看