Javascript uses simple syntax to create objects

Source: Internet
Author: User

 

  <  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 >

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.