coming from clojure I need to write some javascript functions
Lets say I have a function that acts on some spec object.. I want to refer to the variables in spec in a local scope without having to write spec.container, spec.nrow, spec.ncol. It seems there is no native support for destructuring, is this correct? If so
how would I write an unpack (depth 1) function as below? – I don’t really want to use eval in it either.
var spec = {container: {width: 900, height: 600}, nrow: 4, ncol: 5, vgap: "5px"}
function grid(spec) {
// bad..
var width = spec.container.width;
var height = spec.container.height;
var nrow = spec.nrow;
etc...
// I would prefer to write this, then have local access to nrow, width, height, etc
unpack(spec);
unpack(container);
}
Thanks
The
with() { }construct was designed for this, but it’s widely considered a dangerous language element, as the rules for scope-conflict are not well defined.I’d say either use the fully-qualified object reference, or live with the one-by-one assignment.