I think .bind(this) could be added as a (more common?) alternative example to var self = this; Personally I'm most excited about the module system. It's by far the most confusing thing for our students (and hard to google since you end up going down the rabbit hole of build systems etc.)
With the introduction of arrow function you won't need to use .bind(this) as arrow functions capture in their lexical scope the variables/keywords: this, arguments, super. e.g. setTimeout(function(){alert(this.a)}.bind(this)); becomes simply setTimeout(() => alert(this.a));
ES6: The features I'm most excited about
71–80 of 144 posts
Re: ES6: The features I'm most excited about
#72I've just been playing with Promises and like them a lot. But one thing I find strange is that ".then()" creates a new promise, but with no way to reject it. ie. I can't write: return new Promise((resolve, reject) => { // Do some stuff, call resolve()/reject() on success/failure. }).then((step1Val, resolve, reject) => { // Do some stuff, call resolve()/reject() on success/failure. // But this doesn't actually work, b…
I like Jquery's Deferred better then ES6 Promise. Promises lack the `always` callback, they don't have any `progress` events. The spec on mdn[0] doesn't mention asynchronous `then` or `catch` behavior. If the callback in Jquery `Deferred#then` returns a Deferred that deferred will be returned by `then`. // Basic async function, resolves after n milliseconds function wait(n) { var promise = $.Deferred(); setTimeout(pr…
function doSomething() {
return new Promise((resolve,reject)={
setTimeout(reject,100);
})
.catch(() => [])
}
doSomething().then((arg) => console.log(arg));Re: ES6: The features I'm most excited about
#73Re: ES6: The features I'm most excited about
#74Earlier quoted context omitted.
JavaScript is object oriented just like any other language. The difference is that it is prototype-based OO instead of class-based OO. I would say that JavaScript's OO is actually more powerful/flexible than other languages but it is less readable and more error prone if you're not careful - For example, the fact that objects can 'borrow' a method from other object an apply it to themselves is a common source of issu…
It's possible to get power without flexibility. Flexibility is bad -- it creates code that is idiomatic to the person who wrote it. Rust and Go are so exciting because they've intentionally rejected flexibility without compromising power.
One thing I wish JS had is a PEP8-style canonical style document. I would find that more useful than classes.
On classes, I am among the less excited about them; however, we are in a ridiculous situation where there are about 500 different library implementations of class inheritance. Backbone has one. Ember has one. Node has util.inherits. Various transpile-to-JS languages (CS, TS) all have their own slightly-different implementations of the resulting prototype code. There is plainly a need for classes in JS, felt by some of the leading projects in JS land.
ES6 classes at least gives all these disparate implementations a refactoring target. How many of them will get there is another matter.
Re: ES6: The features I'm most excited about
#75Earlier quoted context omitted.
It's possible to get power without flexibility. Flexibility is bad -- it creates code that is idiomatic to the person who wrote it. Rust and Go are so exciting because they've intentionally rejected flexibility without compromising power.
Flexibility is not 'bad' - it's a tradeoff. More flexible languages are better for writing DSLs, etc; but using them means a lot of self discipline regarding code style. One thing I wish JS had is a PEP8-style canonical style document. I would find that more useful than classes. On classes, I am among the less excited about them; however, we are in a ridiculous situation where there are about 500 different library im…
That's bad. Few developers have self-discipline. The ones who do still have different rules than you do. That makes reading, understanding, and modifying their code harder. Sounds bad, doesn't it?
There's a reason the most experienced devs rage against extremely flexible languages like PHP and JavaScript and are excited about new, ultra-rigid languages.
Re: ES6: The features I'm most excited about
#76At least now with classes, users coming from other languages wont be as tempted to make their own completely incompatible object systems.
Re: ES6: The features I'm most excited about
#77Re: ES6: The features I'm most excited about
#78I've just been playing with Promises and like them a lot. But one thing I find strange is that ".then()" creates a new promise, but with no way to reject it. ie. I can't write: return new Promise((resolve, reject) => { // Do some stuff, call resolve()/reject() on success/failure. }).then((step1Val, resolve, reject) => { // Do some stuff, call resolve()/reject() on success/failure. // But this doesn't actually work, b…
.then(val => doSomeStuff())
Thats it. The resulting promise will get resolved/rejected based on the return result of doSomeStuff()You can also use throw to reject a promise manually, or return Promise.reject()
.then(val => {
if (something) return successVal;
else throw new Error("Failure");
});
For more complex scenarios where I need the values from previous actions, I like the forgoing the chaining and using a join helper to unwrap any set of promises as needed: let url = getUrl(resource);
let data = url.then(url => fetch(url))
let updateData = d => _.assign(d, {field: 'newVal'}
let update = join(url, data, (url, data) => sendUpdate(url, updateData(data))Re: ES6: The features I'm most excited about
#79Re: ES6: The features I'm most excited about
#80Wow. I can't believe template strings ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... ) aren't in this list. I'm incredibly excited to start having those at my disposal.
Hm, at first glance I am as well, especially with tagged template strings. Those look particularly simple but powerful. Generally though, is it reasonable for features such as these to be included at the language level? I primarily use ES5 JavaScript so I'm used to pulling in a decent amount of modules (in this case handlebars, underscore, etc.) for things like templating. But I will be relieved to have a language-st…
Admittedly you could trivially extend the string prototype to have something more akin to string.format or printf, which was something you couldn't do in other languages for a long time, making it less of a problem.