JS Tip of the Day: super in Object Literals

super in Object Literals
Version: ES2015
Level: Advanced

You may already be familiar with the use of super in classes. But you may not already know that super can be used in object literals as well. Just like super in classes, it will let you refer to inherited methods over methods of the same name in the current instance.

let fancyObject = {
    toString () {
        return `~*~${super.toString()}~*~`;

console.log(fancyObject.toString()); // ~*~[object Object]~*~

In this example, though fancyObject defined it’s own toString(), it was still able to call its original, inherited (from Object) toString() to get the normal string value for objects before making it “fancy” within its own implementation.

super in object literals only works for referring to inherited members. There is no super() equivalent (super, itself, called as a function) in object literals because object literals have no constructor and that format is only for use in constructors.

More info: