Classes are syntactic sugar over functions and functions are also referred to as "callable" objects. So it is possible to treat a function like an object and give them key / value properties like objects. The static
keyword gives us the ability to assign a key / value property to a class itself, not an instance of that class. This lesson will walk you through using the static keyword and even show how to replicate it with regular functions.
Interestingly enough that extends
has worked just fine with function
s and not with class
es only. Good to know:)
So what if I put the keyword this
inside static function ?
what will be the execute scope of it ?
So what if I put the keyword
this
inside static function ? what will be the execute scope of it ?
Hey doug check out the lessons within this course on using this
. It depends on how the static property is called... this
is determined at runtime. In this scenario, this
is the Square
class.
class Square {
static whoAmI(){
return this
}
}
Square.whoAmI()
Sorry, I spelt your name wrong! Meant to put dung!
"They cannot be called on instances of the class. If you use the new keyword against this, you cannot easily access the static properties." - this could be useful for private methods and props (?) e.g. what problem does this help us solve?