Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save rvanzon/52b124e5a84fb4cead3a to your computer and use it in GitHub Desktop.

Select an option

Save rvanzon/52b124e5a84fb4cead3a to your computer and use it in GitHub Desktop.
Jay-inheritance + Webpack
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
var Person = __webpack_require__(1);
var p = new Person(true);
console.log(p.dance()); // => true
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
__webpack_require__(2);
var Person = Object.extend({
"init" : function (isDancing) {
this.dancing = isDancing;
},
"dance" : function () {
return this.dancing;
}
});
module.exports = Person;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
/**
* Sourced from: https://gist.github.com/parasyte/9712366
* Extend a class prototype with the provided mixin descriptors.
* Designed as a faster replacement for John Resig's Simple Inheritance.
* @name extend
* @memberOf external:Object#
* @function
* @param {Object[]} mixins... Each mixin is a dictionary of functions, or a
* previously extended class whose methods will be applied to the target class
* prototype.
* @return {Object}
* @example
* var Person = Object.extend({
* "init" : function (isDancing) {
* this.dancing = isDancing;
* },
* "dance" : function () {
* return this.dancing;
* }
* });
*
* var Ninja = Person.extend({
* "init" : function () {
* // Call the super constructor, passing a single argument
* this._super(Person, "init", [false]);
* },
* "dance" : function () {
* // Call the overridden dance() method
* return this._super(Person, "dance");
* },
* "swingSword" : function () {
* return true;
* }
* });
*
* var Pirate = Person.extend(Ninja, {
* "init" : function () {
* // Call the super constructor, passing a single argument
* this._super(Person, "init", [true]);
* }
* });
*
* var p = new Person(true);
* console.log(p.dance()); // => true
*
* var n = new Ninja();
* console.log(n.dance()); // => false
* console.log(n.swingSword()); // => true
*
* var r = new Pirate();
* console.log(r.dance()); // => true
* console.log(r.swingSword()); // => true
*
* console.log(
* p instanceof Person &&
* n instanceof Ninja &&
* n instanceof Person &&
* r instanceof Pirate &&
* r instanceof Person
* ); // => true
*
* console.log(r instanceof Ninja); // => false
*/
(function () {
Object.defineProperty(Object.prototype, "extend", {
"value" : function () {
var methods = {};
var mixins = new Array(arguments.length);
for (var i = 0; i < arguments.length; i++) {
mixins.push(arguments[i]);
}
/**
* The class constructor which calls the user `init` constructor.
* @ignore
*/
function Class() {
// Call the user constructor
this.init.apply(this, arguments);
return this;
}
// Apply superClass
Class.prototype = Object.create(this.prototype);
// Apply all mixin methods to the class prototype
mixins.forEach(function (mixin) {
apply_methods(Class, methods, mixin.__methods__ || mixin);
});
// Verify constructor exists
if (!("init" in Class.prototype)) {
throw new TypeError(
"extend: Class is missing a constructor named `init`"
);
}
// Apply syntactic sugar for accessing methods on super classes
Object.defineProperty(Class.prototype, "_super", {
"value" : _super
});
// Create a hidden property on the class itself
// List of methods, used for applying classes as mixins
Object.defineProperty(Class, "__methods__", {
"value" : methods
});
return Class;
}
});
/**
* Apply methods to the class prototype.
* @ignore
*/
function apply_methods(Class, methods, descriptor) {
Object.keys(descriptor).forEach(function (method) {
methods[method] = descriptor[method];
if (typeof(descriptor[method]) !== "function") {
throw new TypeError(
"extend: Method `" + method + "` is not a function"
);
}
Object.defineProperty(Class.prototype, method, {
"configurable" : true,
"value" : descriptor[method]
});
});
}
/**
* Special method that acts as a proxy to the super class.
* @name _super
* @ignore
*/
function _super(superClass, method, args) {
return superClass.prototype[method].apply(this, args);
}
})();
/***/ }
/******/ ]);
var Person = require('./person');
var p = new Person(true);
console.log(p.dance()); // => true
require('jay-inheritance');
var Person = Object.extend({
"init" : function (isDancing) {
this.dancing = isDancing;
},
"dance" : function () {
return this.dancing;
}
});
module.exports = Person;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment