I've got this code:
var a = new Date();
var b = a.toISOString();
var c = b.slice(0, b.indexOf('.'));
var d = c.replace('T', ' ');
console.log('a:', a, '\nb:', b, '\nc:', c, '\nd:', d);
What I'd like to achieve, is all four lines combine into one, without using callbacks, classes or promises, just pure methods chaining.
Just like this i.e.: new Date().toISOString().slice(0, *reference to ISO stringified date*.indexOf('.')).replace('T', ' ');
But the problem is, there's no way that this
keyword could point to/return value before .slice()
method and after .toISOString()
one.
Or maybe it could, using some advanced techniques, but I don't know about them.
So is there any way to achieve what I want without using callbacks/promises? I'm asking for my personal purposes, as a matter of curiosity.
This is currently not possible.
However, there is an ECMAScript proposal called pipeline operator which tries to solve this problem. The exact syntax is currently unsettled and the feature might not make it into the language at all.
Your example could look like this (again, this is just a proposal and does not actually work):
var res = new Date().toISOString() |> (_ => _.slice(0, _.indexOf('.')).replace('T', ' '));
This hardly improves readability, though.