Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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