Javascript object-oriented programming code _ js object-oriented-js tutorial

Source: Internet
Author: User
Javascript object-oriented programming code. For more information about js object-oriented programming, see. The Code is as follows:


Var orchard = function () {// base Type constructor proxy static methods are all on the proxy function
This. constructor & this. constructor. apply (this, arguments );
};
Orchard. extend = function (){
Var parentObj = this;
Var parameters = parentObj. parameters?
ParentObj. parameters. concat (_. toArray (arguments): _. toArray (arguments );
Var thisObj = function () {// inherited Type constructor proxy
Var newparameters = parameters. concat (_. toArray (arguments ));
This. constructor & this. constructor. apply (this, newparameters );
};
_. Extend (thisObj, parentObj );
_. Extend (thisObj. prototype, parentObj. prototype );
ThisObj. parameters = parameters;
ThisObj. base = thisObj. prototype. base = parentObj; // a base-type proxy Function
ThisObj. supper = thisObj. prototype. supper = parentObj. prototype; // The base Type constructor class members are on the constructor.
Return thisObj;
};
Orchard. define = function (object ){
If (typeof object = "undefined") object = {constructor: function (){}};
This. prototype = object. constructor;
This. prototype. constructor = this. prototype;
For (var name in this. base)
If (typeof this [name] === "undefined ")
This [name] = this. base [name];
For (var name in this. supper)
If (typeof this. prototype [name] = "undefined ")
This. prototype [name] = this. supper [name];
For (var I = 0; I <arguments. length; I ++)
_. Extend (this. prototype, arguments [I]);
This. prototype. base = this. base;
This. prototype. supper = this. supper;
This. supper = undefined;
Delete this. supper;
Return this;
};
Orchard. definenew = function (){
Var newclass = this. extend ();
Return define. apply (newclass, arguments );
};


Call:

The Code is as follows:


Var Person = orchard. definenew ({
Constructor: function (name ){
This. name = name;
},
Say: function () {return "Hello, I'm" + name ;}
});
Var aBen = Person. extend ("aBen ");
ABen. define ({
Constructor: function (){
This. supper. apply (this, arguments );
}
});
Var aben = new aBen ();
Alert (aben. say ());


The idea is that the Code has not been verified. You can share your ideas. Haha ~~

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.