Javascript class declaration
Declare function Person (name, age)
{
Num = 5;
This. name = name;
This. age = age;
This.info = function ()
{
Alert ("My information:" + this. name + this. age +"
");
}
}
Alert (Person. num );
Var p1 = new Person ();
Alert (p1.info );
Var p2 = new Person ();
Alert (p2.info );
The variables without this modifier above belong to the class. Similar to static attributes in Java, class names must be called directly.
The variables modified by this below belong to the class Object Attributes and need to be called by the object.
Object methods are generally not declared in this way, because each time an object is new, a memory space will be opened up in the memory, which is relatively memory-consuming.
Therefore, the class addition method is generally inherited:
Person. prototype. walk = function ()
{
Alert (this. name + "walking slowly ");
}
P1. p2 now has the walk method,