3 ms·
It's not a problem if you use objects, and the syntax is essentially the same: const {error, value} = (function(){ return {value, error}; })()
by pornel 12y ago
It's not a problem if you use objects, and the syntax is essentially the same:
const {error, value} = (function(){
return {value, error};
})();
- andrewstuart2 12y agoYeah, that's definitely the alternative I'm advocating for. I've seen many an ES6 blogger decide to use arrays in their examples. It's not quite so grievous in the context of a throwaway example, but for real world usage, it might be more costly to figure out a month or two down the road.
- deleted 12y ago[deleted]
- jrochkind1 12y agoWhat, seriously, the names of your variables matter? And this is seen as a good thing?
- andrewstuart2 12y agoAbsolutely they do. That's the major benefit of object orientation: named properties and methods. Otherwise, we fall into exactly the "boolean trap" that the author links away to. [1] [1] http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-trap.html http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-t...
- jrochkind1 12y agoproperties and methods, yes. But I am not familiar with any other object-oriented language (is JS one now?) where the names of local variables matter. I mean, is 'local' even the right word, when the names chosen as references have semantic impact outside the local scope?
- andrewstuart2 12y agoRight, they don't, and they shouldn't. If I'm not mistaken, we already agree completely. I'm saying that functions should return either primitives or objects that have properties (or keys, since in JS all objects are just hashmaps) that uphold some appropriate abstraction, completely separate from whatever the variables might have been called inside the function body. For example, the return object from some function `getDate()` might have a .year property, or .toUnix method, etc. If all I'm given is an array of arbitrarily typed values, I have solely the array indexes and values (or to go to the docs or implementation) to clue me into the semantic meaning of what was returned.
- bzbarsky 12y agoI'm not sure why you think local variable names are mattering here... What's happening is that this expresion: const { error, value } = obj; where obj is an object will assign obj.error to error and obj.value to value. You could name your locals something else if you wanted, of course: const { error: myError, value: myValue } = obj; will assign obj.error to myError and obj.value to myValue. There just happens to be a shorter syntax for the common const { error: error, value: value } = obj; case.
- jrochkind1 12y agoAh, it's a slice, I get it now, thanks.
- sanxiyn 12y ago{ error, value } is a shorthand for { error: error, value: value }. Rust has an exactly same feature.