Last active
September 21, 2021 02:51
-
-
Save tonkotsuboy/4d9d676cb976d9e3d94f933a10117995 to your computer and use it in GitHub Desktop.
class fields and methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
// public field | |
foo = "public field: 寿司"; | |
// private field | |
#bar = "private field: ラーメン"; | |
// public static field | |
static qux = "public static field: うどん"; | |
// private static field | |
static #corge = "private static field: 麻婆豆腐"; | |
// public method | |
grault() { | |
return console.log("public method: みかん"); | |
} | |
// private method | |
#garply() { | |
return console.log("private method: ぶどう"); | |
} | |
// public static method | |
static waldo() { | |
return console.log("public static method: みかん"); | |
} | |
// private static method | |
static #fred() { | |
return console.log("private static method: ぶどう"); | |
} | |
constructor() { | |
console.log(this.#bar); | |
console.log(MyClass.#corge); | |
this.#garply(); | |
MyClass.#fred(); | |
} | |
} | |
console.log(MyClass.qux); | |
MyClass.waldo(); | |
// 実行してしまうとprivateにつきエラー | |
// console.log(MyClass.#corge); | |
// MyClass.#fred(); | |
const myInstance = new MyClass(); | |
console.log(myInstance.foo); | |
myInstance.grault(); | |
// 実行してしまうとprivateにつきエラー | |
// console.log(MyClass.#bar); | |
// MyClass.#garply(); |
JavaScript class field is now at stage 4 and will be in ES2022💐
public or private
static or instance
field
is now an official specification.
The specification is as follows
https://github.com/tc39/proposal-private-methods
https://github.com/tc39/proposal-class-fields
https://github.com/tc39/proposal-static-class-features
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Safari TP 117 supports static fields and private filed JavaScript.
After all, which browsers are available now? I created a sample code that I could check out.
You can check
public or private,
static or instance,
field or method.