public
Last active

Object destructuring for function parameters

  • Download Gist
es5.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
function ajax(options) {
 
options = Object.extend({
"method": "POST",
"error": function() {},
"success": function() {},
// ...
}, options || {})
 
try {
return options.success(net(options.url, options.method))
} catch (err) {
return options.error(err)
}
 
}
es6.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function ajax({
"url": url,
"method": method = "POST",
"error": error = function() {},
"success": success = function() {}
}) {
 
try {
return success(net(url, method))
} catch (err) {
return error(err)
}
 
}
use.js
JavaScript
1
ajax({ "url": urlToService, "method": "GET" })
function ajax(options) {
    options = options || {};
    var url = options.url;
    var method = options.method || 'POST';
    var error = options.error || function() {};
    var success = options.success || function() {};

    try {
        return success(net(url, method));
    } catch (err) {
        return error(err);
    }
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.