ECMA6 004 class类

//传统类写法
        function Demo(){
            this.name = 'zf';       // 实例化对象的属性
            this.fn = function(){    // 实例化对象的方法
                alert('fn')
            }
        };
        Demo.prototype.age = 18;            // 实例化对象原型的属性
        Demo.prototype.fn2 = function(){    // 实例化对象原型的方法
            alert('fn2');
        };
        let a = new Demo();
        console.log(a);
console.log(a);如下

// ECMA6写法
        class Demo{
            constructor(){
                this.name = 'zf';       // 实例化对象的属性
                this.fn = function(){    // 实例化对象的方法
                    alert('fn')
                }
            };
            fn2(){                // 实例化对象原型的方法
                alert('fn2');
            };
            static fn3(){
                alert('fn3');   // 类私有的属性 实例化对象无法获取它!
            }
        };
          Demo.prototype.age = 18; // 实例化对象原型的属性
          let a = new Demo();
        console.log(a);
console.log(a);如下

注意:
a.fn2(); // 'fn2'
//a.fn3(); // a.fn3 is not a function
Demo.fn3() // 'fn3' static 声明的属性 只有类本身才能调用!
 
原文地址:https://www.cnblogs.com/anduyinglufei/p/10372565.html