Study Notes-JavaScript class (2)

Source: Internet
Author: User
// Define a class
Function testclass (name)
{
// Define an attribute and set the default value
This. Name = Name | "jxh ";

// Define an Object Property
This. internalclass = new internalclass (this. Name );

// Define a method
This. getformatname = function ()
{
VaR fname = This. internalclass. formatname ();
Return fname;
}
}

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

// Inherit the subclass of testclass
Function subjectclass (name)
{
This. base = testclass;
This. Base (name );
}

When inheriting, you are concerned about whether the parent class and the subclass operate on the same object when the parent class has referenced objects. <script language = "JavaScript">
Function click_event ()
{
VaR testclass = new testclass (); // declare the parent class
VaR subjectclass = new subjectclass ("jxhwei"); // declare a subclass

VaR name = "parent class:" + testclass. internalclass. internalname; // call the object attribute of the parent class
Name + = "\ r \ n subclass :";
Name + = subjectclass. internalclass. internalname; // call the object attribute of the subclass.

Alert (name );
}
</SCRIPT>

Running result, OK

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.