I’m coming back to “raw” Javascript, and I’m writing classes like this:
var Person;
Person = (function() {
function Person() {}
Person.prototype.run = function() {};
Person.prototype.jump = function() {};
Person.prototype.talk = function() {};
return Person;
})();
I feel like the repetition of Person.prototype isn’t very DRY: it’s also harder to avoid line wrapping. Are there common ways of addressing this? For example, one could assign Person.prototype to a small variable like cls, which would make the lines look more like
cls.run = function() {};
…but that might be too unconventional.
Since you do not have anything in the prototype you can simply assign a new object to it:
Otherwise you could use a function such as jQuery’s
$.extend()to merge two objects: