Prototype Fun!
by jessekinsman
JavaScript
function foo(who) {
this.me = who;
}
foo.prototype.identify = function() {
console.log("hello this is " + this.me)
}
var a1 = new foo("jesse");
a1.identify();
var a2 = new foo("Bill");
a2.speak = function() {
console.log("this is " + this.me + " speaking");
}
a2.speak();
// Both objects point to the same object
console.log("a2.constructor === a1.constructor: ");
console.log(a2.constructor === a1.constructor);
console.log("a2.constructor === foo ");
console.log(a2.constructor === foo);
// This is the dunder proto object
console.log("a2.constructor.prototype === a2.__proto__ "); console.log(a2.constructor.prototype === a2.__proto__);
// Using Object.getPrototype to get the prototype
console.log("Object.getPrototypeOf(a2) === a2.__proto__ "); console.log(Object.getPrototypeOf(a2) === a2.__proto__);