From 8a9d9de14b430904b2e97f50e486f6e636443ac5 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 15 Nov 2022 13:44:01 +0800 Subject: [PATCH] feat: update es6.md about private class (#76) * feat: update es6 about private class * update --- docs/es6.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/es6.md b/docs/es6.md index 8b87bfb..0db9865 100644 --- a/docs/es6.md +++ b/docs/es6.md @@ -149,6 +149,41 @@ class Circle extends Shape { 原型的语法糖。 请参阅: [类](https://babeljs.io/learn-es2015/#classes) +### 私有类 + +javascript 默认字段是公共的(`public`),如果需要注明私有,可以使用(`#`) + +```js +class Dog { + #name; + constructor(name) { + this.#name = name; + } + printName() { + //只能在类内部调用私有字段 + console.log(`你的名字是${this.#name}`) + } +} + +const dog = new Dog("putty") +//console.log(this.#name) +//Private identifiers are not allowed outside class bodies. +dog.printName() +``` + +#### 静态私有类 + +```js +class ClassWithPrivate { + static #privateStaticField; + static #privateStaticFieldWithInitializer = 42; + + static #privateStaticMethod() { + // … + } +} +``` + Promises -------- @@ -455,6 +490,13 @@ function foo() {} foo.name // "foo" ``` +### length 属性 + +```js +function foo(a, b){} +foo.length // 2 +``` + Objects -------