JSFiddle - React, Tailwind, and code Playground
by Cody Lindley
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
JavaScript
/* Create a View constructor function, sort of mimics what is done with the actual Backbone.View found in Backbone.js */
var View = function (parameter1,parameter2) {
this.IAm = 'View Constructor';
/*call the first initialize function found in the prototype chain and set its scope to this*/
this.initialize.apply(this, arguments); //pass arguments array to initialize function
};
/* Extend the View constructor's prototype with a default empty initialize function to be invoked when no initialize function is passed to the extend() function*/
View.prototype.initialize = function () {};
/*Create a generic static extend() function for creating sub-classes or what I like to call sub-constructors. Include it as a property on the View constructor function*/
View.extend = function (protoProps, staticProps) {
var parent = this; /*this is the View constructor function in this code, or the function that extend is a static property*/
var child; /*this will be the sub constructor function, i.e. sub-constructor or specifially in this code subView*/
if (protoProps && _.has(protoProps, 'constructor')) { /*check for an alternative constructor passed into extend()*/
child = protoProps.constructor; /*if found, use that as the sub-constructor*/
} else {
/*else create a function (i.e. the sub-constructor) that when invoked calls View() constructor passing it arguments from the child function (i.e. the sub-constructor) and setting the context to the child function (i.e. the sub-constructor). This copies over the properties in View constructor*/
child = function () {
return parent.apply(this, arguments);
};
}
/*add the parents static properties and any staticProps provided to extend(), yes this includes extend() itself on the parent, thus subView() now can call extend() and sub-constructing can go on forever*/
_.extend(child, parent, staticProps);
/*Set the prototype chain to inherit from parent...