Skip to content

Instantly share code, notes, and snippets.

@tio-iis
Created April 3, 2022 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tio-iis/5b3604583b8d235187e0fe2f2934225c to your computer and use it in GitHub Desktop.
Save tio-iis/5b3604583b8d235187e0fe2f2934225c to your computer and use it in GitHub Desktop.
class User {
//staticなプロパティ
//Userクラスによって生成されたオブジェクトから利用することはできない。
static staticProperty = "this is staticProperty"
constructor(name) {
//通常のプロパティ
this.name = name
}
//通常のメソッド
normalGetName() {
return this.name + "-san"
}
//staticなメソッド
//Userクラスによって生成されたオブジェクトから利用することはできない。
static staticGetName() {
//thisによってクラスが持つプロパティを参照することはできない。
//return this.name + "-san"
//staticなメソッドを参照することはできる。
console.log(User.myStaticMethod())
//staticなプロパティを参照することはできる。
return User.staticProperty
}
static myStaticMethod() {
return "my static method"
}
//通常のメソッド内でstaticなプロパティとメソッドを利用することができる。
useStaticInNormalMethod() {
return User.staticGetName() + " / " + User.staticProperty
}
}
//var d = Date.now()
console.log(Math.round(0.5))
//var iistio = new User("iistio")
//console.log(iistio.name)
//console.log(iistio.normalGetName())
//var suzuki = new User("suzuki")
//console.log(suzuki.name)
//console.log(suzuki.normalGetName())
//console.log(User.staticGetName())
//console.log(User.staticProperty)
//console.log(iistio.useStaticInNormalMethod())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment