I’m new in javascript.
How can I declare a class in javascript for example with property that named username.
then after username property valued a function is run.
In C# it is something like this :
public int M_A
{
get
{
return m_a;
}
set
{
m_a = value;
RunFunction();
}
}
Javascript doesn’t have a class-based inheritance, it uses a prototype-based. Additionally, it doesn’t provide support for getters and setters (in most versions).
Here would be one way to write your example:
Usage:
Since we’re just approximating a class system, there’s actually a couple ways to do this. Here’s another:
Usage:
For a better understanding of prototypal inheritance and javascript class systems, check out these posts: