HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Minimal but complete AMD implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
butamdcompleteminimalimplementation

Problem

I've got a minimal implementation (under 2k minified) of the Asynchronous Module Definition API. So far it handles all of the required stuff (I think; it passes the relevant unit tests, anyway), and none of the optional stuff.

I haven't looked at any other implementations yet, but I'd love to hear feedback from someone who has.

```
(
/**
amdlite.js

@param {Object} global
@param {undefined=} undefined
*/
function(global, undefined){

'use strict';

/** Modules waiting for dependencies to be exported.

@type {Array.}
*/
var pendingModules = [];

/** New modules since the last script loaded.

@type {Array.}
*/
var newModules = [];

/** Loaded modules, keyed by id.

@type {Object.}
*/
var cache = { };

/** Names of modules which are loading/loaded.

@type {Object.}
*/
var loads = { };

/** Module definition.

@name Module

@constructor

@param {string?=} id
Optional string identifying the module.
@param {Array.?=} dependencies
Optional array of strings identifying the module's dependencies.
@param {function(...)?=} factory
Optional function returning the export value of the module.
@param {?=} exportValue
Optional export value for modules without a factory.
@param {function(Module)?=} generator
Optional function returning a dynamic export value for the module.
*/
function Module(id, dependencies, factory, exportValue, generator) {
this.id = id;
this.dependencies = dependencies;
this.factoryFunction = factory;
this.exports = {};
this.generator = generator;
if (!factory) {
this.exportValue = exportValue || this.exports;
}
}

/** Load dependencies.
*/
Module.prototype.loadDependencies = function () {
var dependencies = this.dependencies;
var id, i, j;

Solution

This code is good

  • JsHint only found 1 unused variable and 1 missing semicolon



  • I grokked most of the code after the first read



  • Well named variables/functions



  • well commented (I am not a big fan of the auto doc comments, but to each their own)



  • The exception to commenting is perhaps the dynamic part towards the bottom which is incidentally the part I still don't grok too well



For your other questions with regards to API, I have no clue.

Context

StackExchange Code Review Q#49879, answer score: 3

Revisions (0)

No revisions yet.