(Just realised I forgot to reply all! Whoops!)
Can't we just use destructuring defaults for this, given it seems that
both V8 and SpiderMonkey lazily invoke functions in this context? I
haven't corroborated this with the spec though, so I could be wrong.
const log = name => fn => (...args) => console.log('Calling', name) ||
fn(...args);
const getFirst = log('getFirst')(() => 'manual first!');
const getSomeObject = () => ({ second: 'second' });
const getFullObject = () => ({ first: 'real first!', second: 'second' });
(() => {
// first is already defined, thus getFirst() isn't called
let { first = getFirst(), second } = getFullObject();
})();
(() => {
// logs 'Calling getFirst'...
let { first = getFirst(), second } = getSomeObject();
})();
We can prove this with a simpler example:
const getThing = () => {
console.log('called');
return 1;
};
(() => {
let { x = getThing() } = { x: 1 };
})();
// Logs nothing
// <= undefined
(() => {
let { x = getThing() } = {};
})();
// Logs 'called'
// <= undefined
I appreciate that one could pipe /n/ functions with your proposal, but
personally I'm not sure this necessitates new syntax.
On 18/11/2019 15:44, Artem Kobzar wrote:
When we need to me destruction from array or object and change value
then we need make something like this
```js
let { first, second } = getSomeObject();
first = computeFirst(first);
// Or create temporary useless variable
const obj = getSomeObject();
const second = obj.second;
const first = computeFirst(obj.first);
```
I propose to make operator which will give ability to us to apply some
function to property inside destruction-syntax without mutable or
temporary variables
```js
const { first | computeFirst, second } = getSomeObject();
```
I think that syntax should be discussed, but it seems lie pipe-symbol
is well-known operator in bash and in AngularJS/Vue community.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss