(One needs some form of browser normalization so that modern features works, no-one is doubting that).
Related: [jQuery library critique][2]
| var Point = { | |
| constructor: function constructor(x, y) { | |
| this.x = x; | |
| this.y = y; | |
| return this; | |
| }, | |
| dist: function dist() { | |
| return Math.sqrt((this.x * this.x) + (this.y * this.y)); | |
| }, | |
| toString: function toString() { |
| /* | |
| Server side tool minimises the path to file to a small string. | |
| It then goes and recompiles every require call to that path to be that string | |
| Hopefully closure compiler will optimize module -> m and optimise m.exports -> m.e | |
| */ | |
| // define | |
| define("/path/to/file", function (require, module) { | |
| // your code should require some other things |
| (function (modules) { | |
| var getBuild = function (build) { | |
| return function (ignore, module) { | |
| module.exports = build.exports; | |
| }; | |
| }; | |
| var getModule = function (scope, tree, path) { | |
| var name, dir, exports = {}, module = { exports: exports }, require, build; | |
| path = path.split('/'); | |
| name = path.pop(); |
| int some_method() { | |
| SOME_STRUCT* reallyBigObject = malloc(sizeof(SOME_STRUCT)); | |
| // FORGET TO DEREFERENCE reallyBigObject | |
| return 42; | |
| } |
| (function () { | |
| /* | |
| totally future proof. | |
| */ | |
| var hostElementPrototype = window.Element && window.Element.prototype; | |
| if (!!hostElementPrototype && ! "nextElementSibling" in hostElementPrototype ) { | |
| // .defineProperty for IE8 & modern, defineProperty shim for legacy non-IE |
Shims allow you to work without a browser compliance or without a browser abstraction library. Modern browsers have enough native and host APIs that you do not need any third party libraries for these features, you just need shims as your legacy browser compliance strategy.
Host objects defined in the WHATWG and W3C specifications are standard APIs. They are well known, readable and maintainable. They are highly optimised and efficient.
Consider getElementsByClassName, you can either use it and have a shim for non-compliant browsers. This gives you maximum performance for compliant browsers, good performance for non-compliant browsers.
| // Original - @Gozola. This is a reimplemented version (with a few bug fixes). | |
| window.WeakMap = window.WeakMap || (function () { | |
| var privates = Name() | |
| return { | |
| get: function (key, fallback) { | |
| var store = privates(key) | |
| return store.hasOwnProperty("value") ? | |
| store.value : fallback | |
| }, |
| var Base = require('./base') | |
| module.exports = Reporter; | |
| function Reporter(runner) { | |
| Base.call(this, runner); | |
| /* | |
| suite: { title: String } | |
| */ |
| // empty suite | |
| var suite = new mocha.Suite(), | |
| BaseReporter = mocha.reporters.Base, | |
| // our runner | |
| Reporter = function () { ... }; | |
| // force tdd (or bdd or exports) style on suite | |
| mocha.interfaces.tdd(suite); | |
| // create runner |