Skip to content

Instantly share code, notes, and snippets.

@creationix
Forked from brianleroux/person.js
Created October 14, 2010 02:50
Show Gist options
  • Save creationix/625450 to your computer and use it in GitHub Desktop.
Save creationix/625450 to your computer and use it in GitHub Desktop.
function Person() {
// properties and validations
this.attr(
{ id: Number, unique: true, nullable: false },
{ email: String, unique: true, nullable: false, min: 1, max: 55, format: '[a-b]' },
{ salt: String },
{ pswd: String },
{ active: Boolean, init: false },
{ tags: Array }
);
// helpful property declarations
this.timestamp();
// callbacks
this.creating({ before: poundSalt, after: emailActivationCode });
this.updating();
this.deleting();
// a private function to generate a unique salt for hashing the password, called in 'creating' callback
// testable therefore by the creating callback?
function poundSalt(obj) {
}
// emails an activation code
function emailActivationCode(obj) {
}
function forgotPassword(obj) {
}
// views, beautiful custom finders
this.view('tags', { map: function () {}, reduce: function () {} });
this.view('popular');
// public instance attributes
return {
// activates the user account
get active() {
},
set active() {
};
}
@isaacs
Copy link

isaacs commented Oct 14, 2010

Sorry, not my intent to be arrogant, or overly critical of a sketch. Just that I have seen a lot of effort spent (by myself and others) on coding pattern non-problems in the past, and eventually came to the conclusion that just using ctor/prototype/new, in the standard way, while not the most pretty, is the least clever and surprising, and the easiest for a newcomer to jump into.

Yes, this is lovely:

function Foo () {
  // init
} << Bar << {
  foo : function () { .. }
}
var f = new Foo()
f instanceof Bar // true
f instanceof Foo // true
f.foo()

and with the hooks JavaScript gives you into the casting process with valueOf, it's entirely doable. (This is an extreme example, to be sure.) While lovely, and I'm sure it'd be fun to spend a saturday implementing it, it's not terribly useful.

Not that all sketches need to be diagrams. Some sketches can just be doodles, and that's perfectly ok. And occasionally something really neat comes out of the exploration.

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