Don't allow overriden methods

This is a little technique I’ve been working on for ensuring that objects don’t get used with overridden methods that shouldn’t be. To put it bluntly: if you’ve ever worried about someone overriding one of your methods through inheritance in JavaScript, here’s the solution.

Basically, the idea is very simple. In the object constructor, just loop through the properties and ensure that the methods you want are, in fact, the methods you want. Consider you have this object:

function MyObject() {
    for (sProp in this) {
        if (sProp.indexOf("_") === 0) {
            if (this[sProp] != arguments.callee.prototype[sProp]) {
                throw new Error("Illegal override of method " + sProp + "().");
            }
        }
    }
}

MyObject.prototype._sayHi = function () {
    alert("hi");
};

I’m using the underscore before the method name to indicate that this method must always be the original one (the one defined on the prototype). Now suppose someone comes along and wants to inherit from this object using the combination hybrid constructor/prototype approach discussed in my book (p. 110), such as:

function MySubObject() {
    MyObject.apply(this);
}

MySubObject.prototype = new MyObject();
MySubObject.prototype._sayHi = function () {
    alert("hola");
};

When someone comes along and tries to create a new instance of MySubObject, they are met with an error stating they’ve illegally overridden a method. Granted, this assumes that you’re using the hybrid inheritance approach (which I still believe is the best approach around), but that’s why coding standards are so important in an organization. If you can assume that subclassing is always done this way, you can also assume a measure of control over methods that should not be overridden.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.