Is there a more elegant way to "fake" class inheritance? Is there a more elegant way to "fake" class inheritance? mongoose mongoose

Is there a more elegant way to "fake" class inheritance?


There are some ways to try and do this, though not sure exactly what your trying to extend.

You can add instance methods <schema>.methods.<mymethod> = function(){}

// define a schemavar animalSchema = new Schema({ name: String, type: String });// assign a function to the "methods" object of our animalSchemaanimalSchema.methods.findSimilarTypes = function (cb) {    return this.model('Animal').find({ type: this.type }, cb);}

And you can add static methods <schema>.statics.<mymethod> = function(){}

// assign a function to the "statics" object of our animalSchemaanimalSchema.statics.findByName = function (name, cb) {    return this.find({ name: new RegExp(name, 'i') }, cb);}var Animal = mongoose.model('Animal', animalSchema);Animal.findByName('fido', function (err, animals) {    console.log(animals);});

Examples are from the mongoose docs - just search for "statics".

The statics functions you can call on a model. The methods are usually functions that work with an instance of a document returned from a query or created with new.