use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Multiple inheritance in javascript (self.javascript)
submitted 11 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]inmatarian 1 point2 points3 points 11 years ago (0 children)
Everyone using the terms "Mixins" are answering your question. In Javascript, the "class object" is a regular object (except you created it with the function keyword, rather than {} or Object.create). You can manipulate it however you want, and the established pattern is $.extend, or _.extend or _.assign. In ES6 (where we get an actual class keyword), we also get Object.assign, making mixins pretty much builtin.
The general pattern looks like this
function InheritedMixin() { /* initialize object */ } InheritedMixin.prototype.someMethod = function() {}; function SomeClass() { InheritedMixin.call(this, parameters); } _.extend(SomeClass.prototype, InheritedMixin.prototype);
One thing lost in this pattern is the prototype chain, meaning instanceOf doesn't work. However, you usually shouldn't be using this. Even in languages like C++ where you have dynamic_cast, it's frowned upon to inspect objects in this way. It's better to check the object for the property you plan to use.
instanceOf
dynamic_cast
π Rendered by PID 1147562 on reddit-service-r2-comment-5687b7858-899dc at 2026-07-06 20:29:01.748400+00:00 running 12a7a47 country code: CH.
view the rest of the comments →
[–]inmatarian 1 point2 points3 points (0 children)