Search code examples
javascriptcoffeescriptcompatibilitykeywordsuper

Is it ok to use the super keyword as a method name?


I see that most frameworks do not use the keyword super on their object, instead they use something like _super

I understand that it's a keyword but is it safe & acceptable to use it as a method name like this

var Class = function() {
   this.super = function(){};
};


var instance = new Class()
instance.super()

It works fine in Chrome with "use strict" mode on.

Side note, I know that coffeescript "escapes" it like this["super"], surely their must be a real reason for that.


Solution

  • In ECMAScript 5, all keywords can be used as property identifiers. Old browsers will fail, so if you need to support old ones, you'd need to use the ["super"] syntax.