2021-10-16

文章探讨了在Chrome74和Node12版本中JavaScript新增的类私有域特性,包括私有变量和私有函数的使用。通过示例展示了如何声明和访问这些私有成员,以及它们在继承、克隆和静态方法中的行为。同时,文章指出了Object.assign和structuredClone在处理私有属性时的限制。
摘要由CSDN通过智能技术生成

chrome 74;node 12 新增了 js 类私有域,包括私有变量,私有函数

class Mtest {
  #privateField;
  #privateMethod() {
    console.log("call #privateMethod()")
    return "[privateMethod " + this.#privateField + "]";
  }
  publicMethod() {
    console.log("publicMethod this.#privateField", this.#privateField)
    return this.#privateMethod();
  }
  static #privateStaticMethod() {
    return "call privateStaticMethod";
  }
  static #PRIVATE_STATIC_FIELD;
  // #constructor > Class constructor may not be a private method
  constructor() {
    console.log("Object.keys(this):",Object.keys(this),"私有属性不能用Object.keys") // []
    this.#privateField = "privateField"
    console.log(`privateField: this["#privateField"]:`, this["#privateField"])
    console.log(`privateField: this.#privateField :`, this.#privateField)
  }

  static publicStaticMethod() {
    Mtest.#PRIVATE_STATIC_FIELD = 42;
    return Mtest.#PRIVATE_STATIC_FIELD;
  }

  static publicStaticMethod1() {
    // 继承后 publicStaticMethod2 中的 this 指向子类 ; 故此不能用this
    return Mtest.#privateStaticMethod();
  }

  static publicStaticMethod2() {
    // 继承后 publicStaticMethod2 中的 this 指向子类 ; 故此不能用this
    return this.#privateStaticMethod();
  }
}


class SubClass extends Mtest {
  #subPrivateField;

  constructor() {
      super();
      // console.log(super.#privateMethod()) Unexpected private field
      this.#subPrivateField = 23;
      // this.#privateField > Private field "#privateField" must be declared in an enclosing class
  }
}


let m = new Mtest()
m.publicMethod();
Object.getOwnPropertyDescriptors(m)  // {} 
console.log(Mtest.publicStaticMethod())
console.log(m)
let clone = {};
Object.assign(clone, m)
Object.setPrototypeOf(clone, Mtest.prototype)

try {
  console.log(clone.publicMethod())
} catch (error) {
  console.log(error.message)
  console.log("Object.assign 不能克隆私有属性");
}

const m1 = structuredClone(m)
console.log("利用structuredClone m 到m1");
console.log("深度克隆不会克隆方法,克隆私有属性 m1.publicMethod == m.publicMethod :",m1.publicMethod == m.publicMethod)
// console.log(m1.publicMethod())
console.log(m1);
Object.setPrototypeOf(m1, Mtest.prototype)

console.log(SubClass.publicStaticMethod1());  // 42
// console.log(SubClass.publicStaticMethod2());  // 异常
console.log("SubClass  publicMethod:",new SubClass().publicMethod())

打印输出:
Object.keys(this): [] 私有属性不能用Object.keys
privateField: this[“#privateField”]: undefined
privateField: this.#privateField : privateField
publicMethod this.#privateField privateField
call #privateMethod()
42
Mtest {}
Cannot read private member #privateField from an object whose class did not declare it
Object.assign 不能克隆私有属性
利用structuredClone m 到m1
深度克隆不会克隆方法,克隆私有属性 m1.publicMethod == m.publicMethod : false
{}
call privateStaticMethod
Object.keys(this): [] 私有属性不能用Object.keys
privateField: this[“#privateField”]: undefined
privateField: this.#privateField : privateField
publicMethod this.#privateField privateField
call #privateMethod()
SubClass publicMethod: [privateMethod privateField]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值