Skip to content

Instantly share code, notes, and snippets.

@mikedidthis
Last active December 21, 2015 21:09
Show Gist options
  • Save mikedidthis/6366607 to your computer and use it in GitHub Desktop.
Save mikedidthis/6366607 to your computer and use it in GitHub Desktop.
How should this be named to make it clear to another developer what is going on. ( Related: http://stackoverflow.com/questions/18481599/replicating-constructors-and-new-with-object-create)
var app = app || {};
// This is the 'model / sigular'
app.Bottle = {
someFunc : function () {
},
someOtherFunc : function () {
}
};
// This does something with all 'Bottle' hence Bottles.
app.Bottles = {
current : [],
create : function ( elems ) {
for (var i = 0, len = elems.length; i < len; i++) {
this.current.push( Object.create( app.Bottle, { 'elem' : { value: elems[ i ] } } ) );
}
}
};
@mikedidthis
Copy link
Author

Usage: app.Bottles.current (lists all current Bottle objects)

So my question is this. How can I make it clear that app.Bottle is a reusable object (model) to be used with app.Bottles. I understand we use _private to mark something as private (As mentioned by @tjcrowder its not truly private). Basically I don't really want app.Bottle being used directly.

I had thought about nesting app.Bottle inside app.Bottles so it becomes app.Bottles.Bottle, but that seems a little silly.

Any ideas?

@rlemon
Copy link

rlemon commented Aug 28, 2013

var bottle = app.Bottles.get(bottleIndex);

@mikedidthis
Copy link
Author

@rlemon I am more interested in the naming at the moment, rather than usage. Is it clear that app.Bottle and app.Bottles are related?

@tjcrowder
Copy link

FWIW, it's clear to me that they're related. It's not clear that you shouldn't use Bottle without using Bottles, though. If you want to do that, make it impossible to create Bottle on its own by adding a factory method to Bottles or some such.

Note that using an underscore on a property doesn't make something private. It does nothing at all other than suggest to someone who uses the same convention that it's meant to be private. You can have truly private properties if you want them (in a couple of different ways), more here.

@eternalruler
Copy link

Perhaps you could place app.Bottle into a subgroup where it is understood that it is a model i.e. app.Model.Bottle.

@rlemon
Copy link

rlemon commented Aug 28, 2013

@mikedidthis then app.Bottles.Bottle is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment