Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Created July 24, 2013 02:32
Show Gist options
  • Select an option

  • Save ryanburnette/6067677 to your computer and use it in GitHub Desktop.

Select an option

Save ryanburnette/6067677 to your computer and use it in GitHub Desktop.
JavaScript prototype pattern.
(function () {
'use strict';
var proto
, bob
, alice
, bobby
;
function Person(name) {
if ( !(this instanceof Person) ) {
return Person.create(name);
}
this.name = name || "(name tag fell off)";
}
proto = Person.prototype;
proto.greet = function (other) {
var me = this
, greetee = other.name || "Someone I don't know"
;
console.log('Hello ' + greetee + ', my name is ' + me.name);
};
Person.create = function (a, b, c) {
return new Person(a, b, c);
};
bob = new Person();
alice = Person.create('Alice');
bobby = Person('Bobby Tables'); // protection from throwing an e from a lack of thisness
bob.greet(alice);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment