Javascript can not only implement inheritance, but sometimes we only need some attributes or methods in the class, so we can consider aggregation to implement
For example, here is just a train of thought. The example is not very detailed. for a deeper understanding of aggregation, you can refer to Baidu or discuss it together in the comments.
Object implementation method:
/*** Metadata classes ** sometimes do not require strict inheritance. What we really need is some functions in a class */(function () {// The var JSON = {toJSONString: function () {var output = []; for (key in this) {output. push (key + "-->" + this [key]);} return output;}/*** aggregate function */function mixin (receivingClass, givingClass) {for (methodName in givingClass) {if (! ReceivingClass [methodName]) {receivingClass [methodName] = givingClass [methodName] ;}} var o ={ name: "jim", age: 16}; mixin (o, JSON); document. write (o. toJSONString (). join (","));
Class implementation
JSON. prototype = {// If prototypetoJSONString: function () {var outPut = []; for (key in this) {outPut. push (key + "-->" + this [key])} return outPut ;}// create the aggregate function mixin (receivingClass, givingClass) {for (methodName in givingClass. prototype) {// if this function does not exist in this class, I will aggregate it. Otherwise, skip if (! ReceivingClass. prototype [methodName]) {receivingClass. prototype [methodName] = givingClass. prototype [methodName] }}// var o = {name: "a", age: 27} var o = function () {this. name = "a"; this. age = 17} mixin (o, JSON); var a = new o (); document. write (. toJSONString (). join (","))