-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6 Default args and interpolation #2015
Conversation
[Something interesting you can do with destructuring](http://babeljs.io/repl/#?experimental=false&evaluate=true&loose=true&spec=false&playground=true&code=var getFileExtension %3D %28src%29 %3D> 'video%2Fmp4'%0Afunction src%28{ src %3D arguments[0]%2C type %3D getFileExtension%28arguments[0]%29 }%29 {%0A console.log%28src%2C type%29%3B%0A}%0A%0Asrc%28'foo'%29%3B%0A%0Asrc%28{%0A src%3A 'foo'%2C%0A type%3A 'bar'%0A}%29): var getFileExtension = (src) => 'video/mp4' // or the real implementation
function src({ src = arguments[0], type = getFileExtension(arguments[0]) }) {
console.log(src, type);
}
src('foo');
// => foo video/mp4
src({
src: 'foo',
type: 'bar'
})
// => foo bar |
7d9b856
to
bd2fb3f
Compare
@@ -165,119 +165,123 @@ function setSelectedOption(target, value) { | |||
target.selectedIndex = i; | |||
} | |||
|
|||
// I know what you're thinking: "This should be a tagged template!". I thought the same thing | |||
// when I first took a look. However, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for suspense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kidding, I think I decided to confirm that I was right and never went back and finished the comment.
Yeah, it's a fun experiment. Not sure how terribly applicable it is to our uses. |
if (!options || options === true) { | ||
options = {}; | ||
} | ||
// Apparently sometimes options is just true, so we need that to make sure we still convert options to an object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Apparently" :-/
Yeah, you can do:
{
children: {
controlBar: true,
posterImage: false
}
}
The false
bit is useful. I think the true might have just been for symmetry. Now that you should be defining children with arrays I think we can ditch the true
option (but not false
).
@heff I think this one's good to go unless you want to take another pass before one of us disappears into the rebase pit |
if (touchDistance > tapMovementThreshold) { | ||
couldBeTap = false; | ||
} | ||
} | ||
}); | ||
|
||
noTap = function(){ | ||
let noTap = ()=> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how I missed these, but they're the same as the other example we reverted. I looked at how babel translates arrow functions and it at least doesn't do an extra fn.bind()
, so that's good. I want to make sure I'm not just being a luddite here so if there's fault in my reasoning let me know. I think it's an important issue.
I still don't think the shortened syntax alone is benefit enough to warrant upping the javascript reading level for contributors. For most contributors JavaScript is probably still not their primary programming lang, and we'd be replacing one of the most most well known keywords with one of the least known that does the exact same thing (in this specific case).
Also, Babel makes named functions when translating arrow functions.
var noTap = function noTap() {}
That goes against the "Arrow functions are always anonymous" rule and could potentially create issues in IE8. Though I'm not sure how likely that is and I wish we could use named functions more for stack traces.
Also if you have a text editor with a function shortcut, there's not much benefit left over for this specific case. f
then tab
quickly in Atom makes an empty function.
I do think there are some cool examples of using arrow functions, like:
let square = x => x * x;
I would have a harder time arguing against that, and it even seems more readable than the example above for some reason.
And using it for it's this
binding.
var boundFn = ()=> {
this.foo();
}
But in all the examples here we seem to just be using it for it's shorter anonymous function syntax, which if that's correct then for consistency there's over 500 other anonymous functions in the codebase we would need to update too. Am I missing another benefit in these examples?
8e39112
to
562ae81
Compare
Trying to rebase from master to pull in #1993 was just uh...not gonna happen. Basically blew everything away and started from scratch, walking through the old diff to make sure I at least got all of the original changes. |
We're in the process of converting Video.js to ES6, and one of our devs assumed that this meant all string concatenation should be replaced with string templates. In some cases though this makes the code less readable. It's more obvious when you're using more complicated variables. ``` const rateName = `${this.player.getPlaybackRate()}x` // vs const rateName = this.player.getPlaybackRate() + 'x' ``` The general rule we landed on was: - When variables are being added to a string, use a string template - When a string is being added to a variable, use concatenation There could be a better way to say that. For the original example, see videojs/video.js#2015 (comment)
@@ -64,9 +64,9 @@ class MuteToggle extends Button { | |||
|
|||
/* TODO improve muted icon classes */ | |||
for (var i = 0; i < 4; i++) { | |||
Lib.removeClass(this.el_, 'vjs-vol-'+i); | |||
Lib.removeClass(this.el_, `vjs-vol-${+i}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+i
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to be positive about things.
Aside from the |
88f9fa0
to
1d94676
Compare
var bar = "foo bat " + +i; Which is functionally the same. TIL. |
woops, didn't mean to close this. |
As well as a some fat arrows thrown in for good measure. Feel free to shoot anything in here you don't like down and we can revert.