Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created August 27, 2010 19:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/553982 to your computer and use it in GitHub Desktop.
Save rwaldron/553982 to your computer and use it in GitHub Desktop.
console.log(blanky); // `object definition`
console.log(typeof blanky); // object
console.log(blanky.__proto__); // object
console.log(typeof blanky.__proto__); // object
console.log(blanky.constructor); // SafetyBlanket()
console.log(typeof blanky.constructor); // function
console.log(blanky.constructor.prototype); // object{}
console.log(typeof blanky.constructor.prototype); // object
// Notice it always comes back to an object
// ------------------------
console.log(blanky.isObject); // true
console.log(typeof blanky.isObject); // boolean
console.log(blanky.isObject.__proto__); // object
console.log(typeof blanky.isObject.__proto__); // object
console.log(blanky.isObject.constructor); // Boolean()
console.log(typeof blanky.isObject.constructor); // function
console.log(blanky.isObject.constructor.prototype); // false {}
console.log(typeof blanky.isObject.constructor.prototype); // object
// Again, it always comes back to an object
// Posted at: http://www.quora.com/How-do-you-implement-object-orientation-in-JavaScript
function SafetyBlanket() {
this.isObject = true;
}
// Declare and assign a new instance
var blanky = new SafetyBlanket();
console.log(blanky); // `object definition`
function SafetyBlanket(material) {
this.isObject = true;
this.madeOf = material;
}
// Extend the prototype with a new method
SafetyBlanket.prototype.tuckIn = function() {
return this.madeOf;
}
// Declare and assign a new instance
var myBlanky = new SafetyBlanket('silk'),
yourBlanky = new SafetyBlanket('fiberglass');
console.log(myBlanky);
console.log(yourBlanky);
console.log(myBlanky.tuckIn());
console.log(yourBlanky.tuckIn());
function Developer(lang) {
this.isObject = true;
this.prefs = {
lang: lang
};
}
Developer.prototype.getPrefs = function () {
return this.prefs;
};
function FrontEnd() {}
function BackEnd() {}
FrontEnd.prototype = new Developer('javascript');
BackEnd.prototype = new Developer('python');
// Reassign the constructor to reflect itself
FrontEnd.prototype.constructor = FrontEnd;
BackEnd.prototype.constructor = BackEnd;
// Extend the prototype with a new method
FrontEnd.prototype.getDOMWindow = function () {
return window;
}
// Extend the prototype with a new method
BackEnd.prototype.getInterpreter = function () {
return this;
}
// Inspect them now, they have the characteristics of
// the Developer object as well as their own methods
console.log(FrontEnd.prototype);
console.log(BackEnd.prototype);
// Declare new instances of our two objects
var frontEndDev = new FrontEnd(),
backEndDev = new BackEnd();
// To be sure, run their methods
console.log(frontEndDev.getDOMWindow());
console.log(backEndDev.getInterpreter());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment