I’m just a newer to JavaScript. I want to write a JS template class like C++. for example:
template <typename T>
class A
{
public:
A(T x)
{
this.a=x;
}
~A()
{
}
void print()
{
std::cout<<a<<std::endl;
}
private:
T a;
};
We can use this class like this:
A<int> test(2);
test.print();
For C++, it’s simple. But in JS, how it can be explained? Thanks very much.
You could do this:
In this case, the variable
ais private, the functionprintis public (as is any other property ofthis), andAis the constructor function for the template (prototype object).