Study Notes-JavaScript class (3)

Source: Internet
Author: User
// Define a class
Function testclass (name)
{
// Total number of initialization + 1
Testclass. Prototype. Count ++ ;

// Define an attribute and set the default value
This . Name = Name |   " Jxh " ;

// Define a static class Property
Testclass. Prototype. Current. internalname =   This . Name;

// Define an Object Property
This . Internalclass =   New Internalclass ( This . Name );

// Define a method
This . Getformatname = Function ()
{
VaR fname= This. Internalclass. formatname ();
ReturnFname;
}
}

// Defines a static attribute for the testclass class.
Testclass. Prototype. Count =   0 ;

// Defines a static class attribute for the testclass class.
Testclass. Prototype. Current =   New Internalclass ( " No instance " );

// Internal class
Function internalclass (name)
{
This. Internalname=Name;
}

For access to static attributes, except for the [class name. prototype. in addition to the access method of attribute name, the [Object Name. attribute name] can be accessed, but because [Object Name. attribute name] If you modify the attribute, [Object Name. attribute name] will not be in the same way as [class name. prototype. attribute names] have the same value, which may cause misunderstanding. Therefore, we recommend that you use only the [class name. prototype. attribute name] One way to access. < Script Language =   " Javascript " >
Function click_event ()
{
Alert ( " No Instance Object " );
Alert ( " Testclass. Prototype. Count: "   + Testclass. Prototype. Count );
Alert ( " Testclass. Prototype. Current: "   + Testclass. Prototype. Current. internalname );

VaR testclass1 =   New Testclass ();
Alert ( " Instance 1 Object " );
Alert ( " Testclass. Prototype. Count: "   + Testclass. Prototype. Count );
Alert ( " Testclass. Prototype. Current: "   + Testclass. Prototype. Current. internalname );

VaR testclass2 =   New Testclass ( " Jxhwei " );
Alert ( " Two objects of the Instance " );
Alert ( " Testclass. Prototype. Count: "   + Testclass. Prototype. Count );
Alert ( " Testclass. Prototype. Current: "   + Testclass. Prototype. Current. internalname );
}
</ Script >

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.