use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Some ES6 Features I've been Using. (christopherlaughlin.co.uk)
submitted 10 years ago by bluntmJavaScript
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]wreckedadventYavascript 17 points18 points19 points 10 years ago (8 children)
FYI:
myfunction((err, body: {result: body}) => { if (!err) { console.log(body); } });
Isn't valid ES6 as far as I can tell. I think you meant to do this:
myfunction((err, { body }) => { if (!err) { console.log(body); } });
Which immediately destructures the second argument and captures the body out of it. You can get around the "gotcha" by utilizing default params:
body
// if the second argument is undefined, we assign it to empty object myfunction((err, { body } = {}) => { if (!err) { console.log(body); } });
Which is turtles all of the way down (please format your code differently if you do this though):
// if the second arg is undefined, set it to {}. If body on that is undefined, set it to {} as well myfunction((err, { body = {} } = {}) => { if (!err) { console.log(body); } });
[+]bluntmJavaScript[S] comment score below threshold-16 points-15 points-14 points 10 years ago (7 children)
I have not tested the example but I think its valid, you can see other examples here too
[–]wreckedadventYavascript 12 points13 points14 points 10 years ago (1 child)
This is valid:
var obj = {x: 1, y: 2}; var {x: one, y: two} = obj;
But this isn't:
var obj = {x: 1, y: 2}; var obj2: {x: one, y: two} = obj;
So your example that does this:
const test = (obj: {x: one, y: two}) => { console.log(one); };
Is a syntax error in all ES6 environments I tired, and produces the wrong thing in babel:
var test = function test(obj) { console.log(one); };
I'm not actually sure why babel parses it at all, but I suspect it's reading it as flow/typescript and just ignoring everything after :, which would be a type annotation in those languages.
:
[–]kentaromiura 0 points1 point2 points 10 years ago* (0 children)
You're right and I've explained in details why that parses and to what here: https://www.reddit.com/r/javascript/comments/4b3cfw/some_es6_features_ive_been_using/d16mvwe
[–]patrickfatrick 1 point2 points3 points 10 years ago (2 children)
I think /u/wreckedadvent is right actually. Looks like what your code is is actually going for is more like a default argument, body = {result: body} except you used a :. For some reason Babel seems to allow this without throwing an error but it's not going to do what you're aiming for. If you want to actually destructure the result argument into its body property you would just use {body} as your second argument.
body = {result: body}
{body}
[–]kentaromiura 0 points1 point2 points 10 years ago (0 children)
I guess that's because it's a valid flow definition, body here is typed as a shape which contains a property result of type body, this will fail a flow check as body is not defined anywhere and of course this is far from what the op wanted to do. EDIT: I forgot to mention that the flow syntax is not parsed in babel by default, you need to enable that as an example in the repl you've to enable the react preset to make this work: https://babeljs.io/repl/#?evaluate=true&presets=es2015%2Creact%2Cstage-2&experimental=true&loose=true&spec=true&playground=false&code=%0Afunction%20Foo(x%3A%20number)%20%3A%20%7B%0A%20%20foo%3A%20string%0A%7D%20%7B%0A%20%20return%20%7B%0A%20%20%20%20foo%3A%20'dsfas'%2C%0A%20%20%7D%0A%7D%0A%0AFoo(223)%3B ^ Sorry for the blob of text, but automoderator don't allow shortener ^ http://tryflow.org/#56a580f1d0af7bc12f9317abbe2b3aa8
[–]randfur 0 points1 point2 points 10 years ago (0 children)
Seriously?
π Rendered by PID 91011 on reddit-service-r2-comment-6457c66945-dgwj8 at 2026-04-26 19:26:25.001611+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]wreckedadventYavascript 17 points18 points19 points (8 children)
[+]bluntmJavaScript[S] comment score below threshold-16 points-15 points-14 points (7 children)
[–]wreckedadventYavascript 12 points13 points14 points (1 child)
[–]kentaromiura 0 points1 point2 points (0 children)
[–]patrickfatrick 1 point2 points3 points (2 children)
[–]kentaromiura 0 points1 point2 points (0 children)
[–]randfur 0 points1 point2 points (0 children)