ES2022

215 阅读1分钟
    //1. .at() //允许下标为负数来访问数组成员
        const arr1=[1,2,3,4,5];
        console.log('at',arr1.at(-1),arr1.at(0));
        //2.数组解构:按下标解构成对象形式
        const {3:third}=arr1; //按下标解构
        console.log('third',third);
        //3.Object.prototype.hasOwnProperty
        //以下只是一个很好的简化
        const x={foo:'bar'}
        const hasOwnProperty=Object.prototype.hasOwnProperty;
        if(hasOwnProperty.call(x,'foo')){
            console.log('hasOwnProperty');
            //是x的自有属性,就执行以下代码
        }
        //4.新规范 hasOwn 方法添加到Object原型中
        const y={foo:'bar'};
        if(Object.hasOwn(x,'foo')){
            //如果foo是x的自有属性,则执行一下代码
            console.log('hasOwn');
        }
        //5.考虑错误提示的三种选择:
            // async function fetchUserPreferences(){
            //     try{
            //         const users=await fetch('/user/preferences').catch(
            //             (err)=>{
            //                 //1.throw new Error('failed to fetch preferences'+err.message);
            //                 //2.const wrapErr=new Error('Failed to fetch preferences');
            //                 // wrapErr.cause=err;
            //                 // throw wrapErr;

            //                 //3.class CustomError extends Error{
            //                 //   constructor(msg,cause){
            //                 //   super(msg);
            //                 //    this.cause=cause
            //                 //}
            //                 //}
            //                     // throw new CustomError('Failed to fetch preferences',err);
            //             }
            //         )
            //     }
            // };
            // fetchUserPreferences()

    //         //新规范
    //         async function fetcUserPreferences() {
    //          try { 
    //                 const users = await fetch('//user/preferences')
    //                 .catch(err => {
    //                     throw new Error('Failed to fetch user preferences, {cause: err});
    //                 })
    // }
    // }
    //     fetcUserPreferences();

        //6.新特性的私有字段(类里面的this可以单独调用的属性和方法,不可以用实例调用拿到)
        class Person {
            #study=0;
            #fastStudy(value){
                console.log(value,'fastStudy.value');
            }
            constructor(){   //实例上的方法
                this.count=0;
                this.learnWay=()=>{
                    console.log('this.count',this.count);
                }
            }
            forGames(){ //实例上的方法
                this.#study++;
                console.log(this.#study,'this.#study');
                this.#fastStudy('lin');
            }
            static reset=1;
            static doReset(){
                console.log(this.count,'this.count111');
            }

        }
        const newPerson=new Person();
        console.log('newPerson.study',newPerson.study); //undefined
        console.log('newPerson.fastStudy',newPerson.fastStudy); //undefined
        console.log('newPerson.count',newPerson.count); //0
        console.log('newPerson.learnWay',newPerson.learnWay); //()=>{console.log('this.count',this.count)}
        console.log('newPerson.forGames',newPerson.forGames);//forGames(){}
        console.log('Person.reset',Person.reset);   //1
        console.log('Person.doReset',Person.doReset);   //doReset(){}
        console.log('Person.study',Person.study);//undefined
        console.log('Person.fastStudy',Person.fastStudy); //undefined
        newPerson.forGames();

        //7.静态块;静态块可以对私有字段的有权力访问
        let getStudyValue;
            class Student{
                #study=0;
                constructor(x){
                    // this.#study=x;
                }
                static{
                    getStudyValue=(a)=>a.#study;
                    console.log('one');
                }
                static{
                    console.log('two');
                }
            }
            const newStudent=new Student('foo');
            //static 块传入实例对象可以特权访问私有字段
            console.log('getStudyValue',getStudyValue(newStudent),'++++',Student);


            class Foo {
  #brand;

  static isFoo(obj) {
    return #brand in obj;  //判断私有字段是否在实例上
  }
}

const j = new Foo();

Foo.isFoo(j); //true
Foo.isFoo({}) //false