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 ~~