ES6 OOP

class ParentIndex{}

//extends继承
//class Index extends ParentIndex
class Index{ 
    
    #name1 = "aaaa"; //私有属性
    
    _const = 1; //实例属性
    const2 = 2; //实例属性
     
    static age = 5; //静态属性
    static #age = 15; //静态私有属性
    
    //构造方法
    constructor(){
        //new.target类自己
        console.log(new.target.name+" CLASS START")
        //name 属性
        console.log(Index.name+" CLASS START")
    }
     
    //取值函数
    get prop() {
        console.log('getter');
    }
    
    //取值函数
    set prop(value) {
        console.log('setter: '+value);
    }
    
    //Generator 方法
    * funX(){
        yield "funx ok"
        return "";
        //console.log("funx ok");
    }
    
    //上面代码中,Foo类的Symbol.iterator方法前有一个星号,表示该方法是一个 Generator 函数。Symbol.iterator方法返回一个Foo类的默认遍历器,for...of循环会自动调用这个遍历器。
    
    //公有方法
    fun1(){
        console.log("fun1 ok");
    }
    
    //私有方法
    #fun(){
        console.log("#fun ok");
    }
    
    //静态方法
    static fun2(){
        console.log("fun2 ok");
    }
    
    //静态私有方法
    static #fun2(){
        console.log("#fun2 ok");
    }
    
    fun3(){
        //检测是否存在#name1私有属性
        console.log(#name1 in this)
    }
    
     
    
    fun2(){
        
        console.log(this._const)
        console.log(this.const2)
        
        console.log(this.#name1)

        console.log(Index.age)
        
        console.log(Index.#age)
        
        this.fun1();
        
        this.#fun()
        
        Index.fun2()
        
        Index.#fun2()
    }
}

let indexObj = new Index();

indexObj.fun2();

indexObj.fun3();

indexObj.prop = 1;

indexObj.prop;

indexObj.funX();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值