< Html >
< Head >
< Title > Javascript uses simple syntax to create objects </ Title >
< Script Type = "Text/JavaScript" >
Window. onload = Function (){
Appendmemberstoobject ();
Simplesyntaxtocreateobject ();
}
Function Appendmemberstoobject ()
{
VaR Testname = " Appendmemberstoobject " ;
Writestart (testname );
// Create an object
VaR Person = New Object ();
// Add a property (public) to an object)
Person. Name = " Lipei " ;
Person. Sex = " Boy " ;
Person. Age = " 24 " ;
// Add a method (public) to an object)
Person. tostring = Function (){
// Both person and this can be used here. In this case, this refers to the person object.
Return Str = " Name: " + This . Name + " , Sex: " + This . Sex + " , Age: " + This . Age;
}
Writeline ( " Person. Name: " + Person. Name );
Writeline ( " Person. tostring (): " + Person. tostring ());
Writeend ();
/*
According to the results, we can add members (attributes, methods, etc.) to the object after the object is created in JavaScript ).
*/
}
Function Simplesyntaxtocreateobject ()
{
VaR Testname = " Simplesyntaxtocreateobject " ;
Writestart (testname );
// Use simple syntax to create an object
VaR Computer = {
CPU: " Core 2 P series " ,
Mainboard: " Asus er " ,
Keyboard: " Shuangfeiyan " ,
Tostring: Function (){
Return Str = " CPU: " + This . CPU + " , Mainboard: " + This . Mainboard + " , Keyboard: " + This . Keyboard;
}
};
Writeline ( " Computer. mainboard: " + Computer. mainboard );
Writeline ( " Computer. tostring (): " + Computer. tostring ());
Writeend (testname );
/*
According to the results, the simple JavaScript syntax can only identify the type after the variable colon and define the type of the variable accordingly.
*/
}
/* ************************ Test tools ************** *********** */
Function Writeline (STR)
{
Document. Write (Str + " <Br/> " );
}
Function Writestart (STR)
{
Writeline ( " Test: " + Str + " <Br/> " );
}
Function Writeend ()
{
Writeline ( " <Br/> <HR/> " );
}
</ Script >
</ Head >
< Body >
</ Body >
</ Html >