Skip to content

Instantly share code, notes, and snippets.

@ewjoachim
Last active August 29, 2015 14:02
Show Gist options
  • Save ewjoachim/3cb2900d9fd3ce998580 to your computer and use it in GitHub Desktop.
Save ewjoachim/3cb2900d9fd3ce998580 to your computer and use it in GitHub Desktop.
Base class for Javascript
define(["jq"], function($){
"use strict";
var Class = function(){};
var minimal_class_definition =
{
init: function()
{
}
};
var extend = function (base_class, additionnal_definition)
{
var _class_definition = $.extend({}, base_class._class_definition, additionnal_definition);
var new_class = function(){
$.extend(this, _class_definition);
this.init.apply(this, arguments);
};
new_class._class_definition = _class_definition;
return new_class;
};
Class = extend(Class, minimal_class_definition);
Class.extend = extend;
return Class;
}
);
// a class wouls look like that :
/*global define */
define(["Class", "jquery"],
function(Class, $){
"use strict";
var ClassName = Class.extend(Class,
{
init: function()
{
},
}
);
return ClassName;
}
);
@unscriptable
Copy link

function Thing (foo) {
    this.foo = foo;
    Base.apply(this); // invoke Base constructor
}

// inherit properties and methods from Base
Thing.prototype = Object.create(Base.prototype);

// override Base's bar method
Thing.prototype.bar = function (a, b) {
    return Base.prototype.bar.call(this, a, b) + ' overridden';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment