prototype testing

Little tests to see how the browser responds to prototype, instanceof and getPrototypeOf.

by paulbruno

JavaScript

function First (name) {
    this.name = name;
}

First.prototype.getName = function () {
    return this.name;
}

function Second (name, age) {
    this.name = name;
    this.age = age;
}

Second.prototype = new First();

var firstObj = new First("Jimmy");
var secondObj = new Second("Sally", 21);

if (firstObj instanceof First) {
    alert("firstObj is an instance of First");
}

if (secondObj instanceof Second) {
    alert("secondObj is an instance of Second");
}

if (secondObj instanceof First) {
    alert("secondObj is an instance of First");
}

if (Second.prototype.constructor === Second) {
    alert("Wait, how'd this happen?");
}

if (Second.prototype.constructor === First) {
    alert("As I thought it should be...");
}

if (Object.getPrototypeOf(secondObj) === Second.prototype) {
    alert("Interesting, very interesting.");
}

if (secondObj.constructor === Second) {
    alert("Man, it'll be screwy if this shows up.");
}

if (secondObj.constructor === First) {
    alert("My fears are allayed.");
}