On gists
OOP JS Class - static properties
JavaScript-OOP
JavaScript
static.js
Raw
#
class Item {
static count = 0
static count2 = 0
constructor(name) {
this.name = name
this.constructor.count++
Item.count2++
console.log(this)
console.log('-----------------------------------------')
console.log(this.constructor)
}
static getCount() {
return Item.count
}
static getCount2() {
return Item.count2
}
}
let A = new Item("A");
let B = new Item("B");
/*
console.log(A.getCount()) // nelze
console.log(B.getCount2()) // nelze
*/
console.log(Item.getCount()) // 2
console.log(Item.getCount2()) // 2
/*
[object Object] {
name: "A"
}
"-----------------------------------------"
class Item {
static count = 0
static count2 = 0
constructor(name) {
this.name = name
this.constructor.count++
Item.count2++
window.runnerWindow.proxyConsole.log(this)
window.runnerWindow.proxyConsole.log('-----------------------------------------')
window.runnerWindow.proxyConsole.log(this.constructor)
}
*/