js Class私有属性

class Person {
            //  声明

            //公有属性
            name;
            gf
            //私有属性
            #age;
            #weight;
            #bf
            //构造方法
            constructor(name, age, weight) {
                this.name = name;
                this.#age = age;
                this.#weight = weight;
                this.#bf='lijongsuk'
                this.gf='yoone'
            }

            //普通方法
            intro() {
                console.log(this.name);
                console.log(this.#age);
                console.log(this.#weight);
            }
        }

        //实例化
        const girl = new Person("小可爱", 18, "45kg");
        console.log(girl.gf) //yoone
        console.log(girl.bf) //访问不了 undefined
        girl.intro();

猜你喜欢

转载自blog.csdn.net/weixin_38128649/article/details/126807138