Closure
Function person (firstname, lastname, age)
{
// Private variable:
VaR _ firstname = firstname;
VaR _ lastname = lastname;
// Public variables:
This. Age = age;
// Method:
This. getname = function ()
{
Return (firstname + "" + lastname );
};
This. sayhello = function ()
{
Alert ("Hello, I'm" + firstname + "" + lastname );
};
};
VaR billgates = new person ("bill", "Gates", 53 );
VaR stevejobs = new person ("Steve", "Jobs", 53 );
Billgates. sayhello ();
Stevejobs. sayhello ();
Alert (billgates. getname () + "" + billgates. Age );
Alert (billgates. firstname); // Private variables cannot be accessed here
Prototype
Function circle (X, Y, R ){
This. x = X;
This. Y = y;
This. r = R;
// This. prototype = NULL;/* The Code can be considered as implicit. Because there is no difference between the definition of "class" in JavaScript and the definition structure of the function, we can say that, all functions have such an attribute hidden. */
}
Circle. Prototype. Area = function (){
Return this. R * This. R * 3.14159;
}
VaR CIRC = new circle (0, 0, 2 );
Alert (CIRC. Area ());
Http://www.cnblogs.com/gwazy/archive/2009/11/07/1598046.html reprinted