JavaScript Object-oriented programming code _JS object-oriented

Source: Internet
Author: User
Copy Code code 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 () {//Inheritance 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; Proxy functions for base types
Thisobj.supper = ThisObj.prototype.supper = Parentobj.prototype; Constructor class members of the base type are all 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:
Copy Code code 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 this, the code has not been validated. The idea of sharing, everyone to see To do. 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.