How do you prefer to create classes? Thoughts on my method?


How do you prefer to create classes? Thoughts on my method?

I don't really like ES6 classes. The system for creating static/class properties is clunky, which is intentional, but I find this to be a big shortcoming in my work.

So thinking back to pre-ES6 times, how do you prefer to define classes?

As a lone JS developer don't have too many opportunities to get code reviews and feedback from others. I'd welcome thoughts/criticisms on my method which integrates with Mithril.js:

var Project = (function(){ // Everything is private, unless attached to $Class or $Instance var $Class = {}; var $Instance = {}; var $Instance_Constructor; var $DOM_Constructor; $Class.all = []; // Class methods $Class.new = function(){ var project = Object.create($Instance); $Instance_Constructor.apply(project, arguments); $DOM_Constructor.apply(project); $Class.all.push(project); return project; } $Instance_Constructor = function(title){ var project = this; project.title = title; } // Instance methods $Instance.mountTo = function(element){ var project = this; return m.mount(element, project.DOM); } // Creates a unique Mithril component for each instance $DOM_Constructor = function(){ var project = this; project.DOM = { view: function(){ return [ m('h1', project.title) ] } } } return $Class; })(); document.addEventListener('DOMContentLoaded', function(){ var project = Project.new('My sweet project'); project.mountTo(document.getElementById('project')); }); 

Submitted July 13, 2017 at 10:42PM by robertgfthomas
via reddit http://ift.tt/2t7aoO2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s