You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 函数实现functionprivate(){leta='我是私有变量';this.getName=function(){returna;}}constp=newprivate()console.log(p.a)console.log(p.getName())// class 实现classprivate1{constructor(){leta='我是私有变量';this.getName=function(){returna;};}}constp1=newprivate1()console.log(p1.a)console.log(p1.getName())
通过函数的创建形式:
另外在这里讲一下网上一种错误的实现方案,通过配置 defineProperty 的不可枚举,不可修改:
配置不可枚举只是让某个属性不能通过
for..in
或Object.keys()
的方式遍历出来,直接user.name
一样还是可以访问到这个属性。The text was updated successfully, but these errors were encountered: