Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Last active September 21, 2021 02:51
Show Gist options
  • Save tonkotsuboy/4d9d676cb976d9e3d94f933a10117995 to your computer and use it in GitHub Desktop.
Save tonkotsuboy/4d9d676cb976d9e3d94f933a10117995 to your computer and use it in GitHub Desktop.
class fields and methods
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();
@tonkotsuboy
Copy link
Author

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.

@tonkotsuboy
Copy link
Author

tonkotsuboy commented May 6, 2021

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

#JavaScript #ES2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment