3 ms·
> var's function scope is a feature! You don't have to place variable declarations in the header! (they are hoisted) Placing the var declarations where they are
by minitech 10y ago
> var's function scope is a feature! You don't have to place variable declarations in the header! (they are hoisted) Placing the var declarations where they are used makes the code easier to understand.
For the vast majority of variables, you don’t need hoisting (i.e. you can declare them in the appropriate scope and only end up using them after the declaration).
> The point of constructor functions is not having to write new.
Um, what? This is completely wrong. You have to write new with constructor functions unless you specifically add code to check whether you’re in a constructor and re-call with new, which is bad practice.
> Async programming is hard, but not because of callbacks. Promises is just a wrap around callbacks, witch just adds complexity and more boilerplate. It will get better with async/await but I will still prefer simple callbacks.
No, async/await is just a wrapper around promises. Promises are pretty great overall, and if your Promise code looks like callbacks then you might be using them wrong. (I would always use bluebird[1] over native promises, though.)
I agree with you that large parts of ES6 add unnecessary complexity, but I don’t think your examples are well-chosen.
[1] http://bluebirdjs.com/ http://bluebirdjs.com/
- z3t4 10y ago> Um, what? Sorry for the confusion, the ES5 example looked like a "factory" function, so I assumed that was what he meant. You do not have to wrap the constructor function in another function! ... function Animal(name) { // This code is run when the object is created. No need to wrap another function around Foo var animal = this; animal.name = name; } Animal.prototype.speak = function speak() { var animal = this; console.log(animal.name + ' makes a noise.'); } Here is a "factory" function: function AnimalFactory(name) { var animalName = name; function speak() { console.log(animalName + ' makes a noise.'); } return {speak: speak} } var animal = AnimalFactory('Animal'); // You can omit new animal.speak(); // animal makes a noise setTimeout(animal.speak, 1000); // you can also do this