Object.prototype.foo(){} kills kitteh example?

59 views
Skip to first unread message

Gavin van der Merwe

unread,
Mar 26, 2013, 5:30:23 PM3/26/13
to mongoo...@googlegroups.com
Fellow javascript coders,

Perhaps it is my lame ignorance but why do custom Object prototypes crash the kitty example copy/pasted from the mongoose website? I have waded deep enough to know that this is mixin behaviour from schema.js designed to augment "Objects" to be persistable into mongo which is fine but why is this not an elective based on how I scope and declare the schema? Kind of feels like a deal breaker because of the all-or-nothing mixin approach that happens globally. 

1. Are Object.prototype extensions bad? (I am exposing "reflective" and typing extensions like hasFunction, getMembers, toArray, toInteger etc)
2. Why does mongoose augment the object prototype globally? Declaritive functional based scoping for the schema via the mongoose api would reduce total memory footprint of native types(in this case object) which is fair because not every implementation of Object ends up in mongo.
3. Am I just doing it wrong? Fail.  

Look forward to hearing from you guys because I would really like to support this framework in my development going forward. Would also be happy to submit a patch if you guys were that way inclined :)

Ciao 

Gav
 

Aaron Heckmann

unread,
Apr 2, 2013, 3:13:57 PM4/2/13
to mongoo...@googlegroups.com
Mongoose does not augment the global prototype. The behavior you are seeing is not unique to mongoose. In JavaScript, manipulating Object.prototype impacts _all_ objects. Unless you make the addition non-enumerable you'll see bugs popping up anywhere a `for in` loop is used to iterate an object.

In general mongoose should not be iterating objects using `for in` but in some cases we must do so to guarantee index key ordering and a few others.

In general, its safer practice to write your libs not to augment global prototypes and instead create subclasses or class wrappers etc that you pass your specific objects into. For example, instead of adding `isArray`, `toInteger` to Object.prototype, create a helper module that you pass the arguments into:

// object-helpers.js
exports.isArray = function (obj) {
  return Array.isArray(obj);
}
exports.toInteger = function (obj) {
  // test for a number and convert obj etc
}

Then include your helper module in your project:

// project.js
var helper = require('./object-helpers.js');
helper.isArray([]) // true
helper.isArray({}) // false

For general purpose helpers search npmjs.org for something like underscore or lodash to get some more ideas.



--
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
 
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Gavin van der Merwe

unread,
Apr 3, 2013, 4:41:29 AM4/3/13
to mongoo...@googlegroups.com
Thank you very much for your response. I realised this weekend I AM SIMPLY DOING IT WRONG :)

Hence my approach to rewrite my project using CoffeeScript without prototype extensions for native types anywhere. 

I am very new to programming server side javascript and in principle have been using statically typed languages so the temptation to extend native types was a little overwhelming and a "good way" of breaking other frameworks. Hope others learn from this :)


---
You received this message because you are subscribed to a topic in the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-orm/cFBi5_m2Lrw/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mongoose-orm...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages