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
  • 相关阅读:
    Constraint.constant动画效果
    poj3469 Dual Core CPU --- 最小割
    开发Blog整理
    Android 四大组件学习之BroadcastReceiver四
    在光标处插入指定文本(支持文本域和文本框)
    图片显示插件
    Extjs4 自定义组件
    Windows英文版GitHub客户端使用操作流程图文攻略教程现没中文版
    innerHTML和innerText怎么区分
    button和input type=button的区别及注意事项
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/6128130.html
Copyright © 2011-2022 走看看