JavaScript Object-oriented four inheritance _js object-oriented

Source: Internet
Author: User
Tags extend inheritance
Copy Code code as follows:

var jsobject = {} | | New Object ();
Jsobject.extend = function (subclass, superclass) {
First, determine if the subclass subclass is already defined, or redefine the class if it is undefined.
if (typeof subclass = = "undefined") subclass = function () {};
If the parent class superclass is a class, it is converted into an object
if (typeof superclass = = "function") superclass = new Superclass ();
Traversing properties and methods in the parent class superclass object
For (var p in superclass)
{
/* Copy properties and methods from the parent class superclass object into the subclass prototype object.
Therefore, the subclass has all attributes of the parent class, that is, inheritance.
SUBCLASS.PROTOTYPE[P] = superclass[p];
}
return subclass;
};
function Student ()
{
THIS.name = "John";
This.updatename = function (name) {
THIS.name = name;
}
}
function Class1 ()
{
This.sex = "male";
This.updatesex = function (Sex) {
This.sex = sex;
}
}
Define class CLASS1 inherit student class
Class1 = Jsobject.extend (Class1, Student);
var obj = new Class1 ();
alert (obj.sex);
alert (obj.name);
Obj.updatesex ("female");
Obj.updatename ("Mary");
alert (obj.sex);
alert (obj.name);

The results show: Male, John, female, Mary
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.