17
loading...
This website collects cookies to deliver better user experience
#
prefix._variableName
, it means we can use that variable only in the class.class GetDateTime {
_start = 0
getDate() {
if(true) {
this._start = new Date()
}
}
}
_start
variable accessible publicly. Checkout here 👇let date = new GetDateTime()
console.log(date._start)
// Thu Jun 24 2021 16:36:06 GMT+0530 (India Standard Time)
#
to create private variables._
with #
class GetDateTime {
#start = 0
getDate() {
if(true) {
this.#start = new Date()
}
}
}
let date = new GetDateTime()
console.log(date.#start)
// Uncaught SyntaxError: Private field '#start' must be declared in an enclosing class
class GetDateTime {
#start = 0
getDate() {
if(true) {
return this.#getNow()
}
}
#getNow() {
this.#start = new Date()
}
}
let date = new GetDateTime()
console.log(date.getDate())
// Thu Jun 24 2021 16:55:32 GMT+0530 (India Standard Time)
class GetDateTime {
static #start = 0
static getDate() {
if(true) {
this.#start = new Date()
return this.#start
}
}
}
console.log(GetDateTime.getDate())
// Thu Jun 24 2021 17:53:02 GMT+0530 (India Standard Time)
👩🏻💻 suprabha.me |