Skip to content

Instantly share code, notes, and snippets.

@Athorcis
Last active March 6, 2017 16:20
Show Gist options
  • Save Athorcis/331ca210484be6386db6 to your computer and use it in GitHub Desktop.
Save Athorcis/331ca210484be6386db6 to your computer and use it in GitHub Desktop.
/* eslint-env amd, commonjs */
/* eslint no-invalid-this: "off" */
'use strict';
(function (global, factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function') {
define(factory);
} else {
global.Class = factory();
}
}(this, function () {
var arr = [],
slice = arr.slice,
hasOwn = arr.hasOwnProperty;
function isFunction(value) {
return typeof value === 'function';
}
function isObject(value) {
return typeof value === 'object';
}
function noop() {
// For classes with no constructor
}
function extend(target) {
var i, length, object, key,
objects = slice.call(arguments, 1);
for (i = 0, length = objects.length; i < length; ++i) {
object = objects[i];
for (key in object) {
if (hasOwn.call(object, key)) {
target[key] = object[key];
}
}
}
return target;
}
/**
* Déclare une classe
* @returns {Class} La classe qui vient d'être déclarée
*/
function declare() {
var constructor, __construct,
__abstract = false;
constructor = function () {
if (constructor.__extends) {
return;
}
if (__abstract && this.constructor === constructor) {
throw new Error('cannot instantiate abstract class');
}
__construct.apply(this, arguments);
};
constructor.define = function (Parent, definition) {
var __static, prototype, key;
if (!isObject(definition)) {
definition = isObject(Parent) ? Parent : {};
if (!isFunction(Parent)) {
Parent = Object;
}
}
if (isFunction(definition.__construct)) {
__construct = definition.__construct;
delete definition.__construct;
} else {
__construct = noop;
}
if (isObject(definition.__static)) {
__static = definition.__static;
delete definition.__static;
}
Parent.__extends = true;
prototype = new Parent();
delete Parent.__extends;
delete constructor.define;
extend(constructor, __static);
constructor.prototype = extend(prototype, definition);
for (key in prototype) {
if (hasOwn.call(prototype, key) && prototype[key] === undefined) {
__abstract = true;
break;
}
}
return constructor;
};
return constructor;
}
/**
* Définit une classe
* @param {Class} [Parent=Object] La classe parent
* @param {Object} [prototype={}] Le prototype de la classe)
* @returns {Class} La classe qui vient d'être définie
*/
function define(Parent, prototype) {
return declare().define(Parent, prototype);
}
return {
declare: declare,
define: define
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment