Whilst I wait for better support for fat arrow functions, they can be emulated with what I like to call an immediately bound function expression (IBFE).
For example this:
var test = (a, b, c) => console.log(a, b, c, this.xyz);Can be emulated with:
| function subRunner(gen) { | |
| const state = gen.next(); | |
| const thisPromise = Promise.resolve(state.value); | |
| return state.done ? thisPromise : thisPromise.then(subRunner(gen)); | |
| } | |
| function asyncWrapper(genFunc) { | |
| return function () { | |
| try { |
| const promises = ['some promises in here']; | |
| const waitForPromises = asyncRunner(function* (promises) { | |
| for (const promise of promises) { | |
| yield promise; | |
| } | |
| // At this point all the promises have resolved. | |
| }); |
| function makeUuid(){ | |
| var randomValues = window.crypto.getRandomValues(new window.Uint8ClampedArray(16)); | |
| var randomHex = ''; | |
| for (var i = 0; i < randomValues.length - 1; i++) { | |
| var hex = randomValues[i].toString(16); | |
| if (hex.length === 0) { | |
| randomHex += '00'; | |
| } else if (hex.length === 1) { |
| // brfs is needed to use this anti-shim. | |
| var fs = require('fs'); | |
| var functionBody = [ | |
| 'var window = {};', | |
| fs.readFileSync(__dirname + '/path/to/node_modules/fetch/fetch.js', 'utf8'), // Fix the path for your needs. | |
| 'return window.fetch;' | |
| ].join('\n'); | |
| // Assigns a function fetch function to module.exports. Here I elect to use a native Promise implementation (or | |
| // polyfilled). This is trivial to adapt into a library that can take a user defined Promise. |
Whilst I wait for better support for fat arrow functions, they can be emulated with what I like to call an immediately bound function expression (IBFE).
For example this:
var test = (a, b, c) => console.log(a, b, c, this.xyz);Can be emulated with:
| var a = true; | |
| var test = () => console.log(this.a); | |
| test(); // true | |
| var test2 = test.bind({ a: false }); | |
| test2(); // Canary: false, Firefox: true |
| // Methods will be bound. This allows them to be public or private | |
| // later on without needing `this`. These can be required in etc. | |
| function addToCounterMixin(context, num) { | |
| context.counter += num; | |
| } | |
| // Use this to proxy a field on from one object to another. | |
| function proxy(context, obj, fieldName) { | |
| Object.defineProperty(obj, fieldName, { | |
| set: function (val) { |
I hereby claim:
To claim this, I am signing this object:
| var config = require('minimist')(process.argv.slice(2)); | |
| module.exports = config; | |
| var errors = []; | |
| if (!config.hasOwnProperty('twitter-consumer-key')) { | |
| errors.push('No twitter-consumer-key given.'); | |
| } |
| import 'dart:io' show HttpRequest, HttpClient, HttpServer, ContentType; | |
| import 'dart:convert' show JSON; | |
| void handleRequest(HttpRequest request) { | |
| // For convenience. | |
| var response = request.response; | |
| // No path is expected. Return with 404 for anything else. | |
| if (request.uri.path != '' && request.uri.path != '/') { | |
| response.statusCode = 404; |