[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Zubair Ahmed via Digital Information World
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
|
Unique stacked-accordion of projects in this One Page portfolio for architect Jacqui Nguyen.
ES6 brought a number of new features to the JavaScript language. Two of these features, generators and iterators, have substantially changed how we write specific functions in more complex front-end code.
While they do play nicely with each other, what they actually do can be a little confusing, so let’s check them out.
Iteration is a common practice in programming and is usually used to loop over a set of values, either transforming each value, or using or saving it in some way for later.
In JavaScript, we’ve always had for loops that look like this:
for (var i = 0; i < foo.length; i++){
// do something with i
}
But ES6 gives us an alternative:
for (const i of foo) {
// do something with i
}
This is arguably way cleaner and easier to work with, and reminds me of languages like Python and Ruby. But there’s something else that’s pretty important to note about this new kind of iteration: it allows you to interact with elements of a data set directly.
Imagine that we want to find out if each number in an array is prime or not. We could do this by coming up with a function that does exactly that. It might look like this:
function isPrime(number) {
if (number < 2) {
return false;
} else if (number === 2) {
return true;
}
for (var i = 2; i < number; i++) {
if (number % i === 0) {
return false;
break;
}
}
return true;
}
Not the best in the world, but it works. The next step would be to loop over our list of numbers and check whether each one is prime with our shiny new function. It’s pretty straightforward:
var possiblePrimes = [73, 6, 90, 19, 15];
var confirmedPrimes = [];
for (var i = 0; i < possiblePrimes.length; i++) {
if (isPrime(possiblePrimes[i])) {
confirmedPrimes.push(possiblePrimes[i]);
}
}
// confirmedPrimes is now [73, 19]
Again, it works, but it’s clunky and that clunkiness is largely down to the way JavaScript handles for loops. With ES6, though, we’re given an almost Pythonic option in the new iterator. So the previous for loop could be written like this:
const possiblePrimes = [73, 6, 90, 19, 15];
const confirmedPrimes = [];
for (const i of possiblePrimes){
if ( isPrime(i) ){
confirmedPrimes.push(i);
}
}
// confirmedPrimes is now [73, 19]
This is far cleaner, but the most striking bit of this is the for loop. The variable i now represents the actual item in the array called possiblePrimes. So, we don’t have to call it by index anymore. This means that instead of calling possiblePrimes[i] in the loop, we can just call i.
Behind the scenes, this kind of iteration is making use of ES6’s bright and shiny Symbol.iterator() method. This bad boy is in charge of describing the iteration and, when called, returns a JavaScript object containing the next value in the loop and a done key that is either true or false depending on whether or not the loop is finished.
In case you’re interested in this sort of detail, you can read more about it on this fantastic blog post titled Iterators gonna iterate by Jake Archibald. It’ll also give you a good idea of what’s going on under the hood when we dive into the other side of this article: generators.
Continue reading %ES6 Generators and Iterators: a Developer’s Guide%
Destructuring assignment sounds complex. It reminds me of object-oriented terms such as encapsulation and polymorphism. I’m convinced they were chosen to make simple concepts appear more sophisticated!
In essence, ECMAScript 6 (ES2015) destructuring assignment allows you to extract individual items from arrays or objects and place them into variables using a shorthand syntax. Those coming from PHP may have encountered the list() function, which extracts arrays into variables in one operation. ES6 takes it to another level.
Presume we have an array:
const myArray = ['a', 'b', 'c'];
We can extract these values by index in ES5:
var
one = myArray[0],
two = myArray[1],
three = myArray[2];
// one = 'a', two = 'b', three = 'c'
ES6 destructuring permits a simpler and less error-prone alternative:
const [one, two, three] = myArray;
// one = 'a', two = 'b', three = 'c'
You can ignore certain values, e.g.
const [one, , three] = myArray;
// one = 'a', three = 'c'
or use the rest operator (...) to extract remaining elements:
const [one, ...two] = myArray;
// one = 'a', two = ['b, 'c']
Destructuring also works on objects, e.g.
var myObject = {
one: 'a',
two: 'b',
three: 'c'
};
// ES5 example
var
one = myObject.one,
two = myObject.two,
three = myObject.three;
// one = 'a', two = 'b', three = 'c'
// ES6 destructuring example
const {one, two, three} = myObject;
// one = 'a', two = 'b', three = 'c'
In this example, the variable names one, two and three matched the object property names. We can also assign properties to variables with any name, e.g.
const myObject = {
one: 'a',
two: 'b',
three: 'c'
};
// ES6 destructuring example
const {one: first, two: second, three: third} = myObject;
// first = 'a', second = 'b', third = 'c'
More complex nested objects can also be referenced, e.g.
const meta = {
title: 'Destructuring Assignment',
authors: [
{
firstname: 'Craig',
lastname: 'Buckler'
}
],
publisher: {
name: 'SitePoint',
url: 'http://www.sitepoint.com/'
}
};
var {
title: doc,
authors: [{ firstname: name }],
publisher: { url: web }
} = meta;
/*
doc = 'Destructuring Assignment'
name = 'Craig'
web = 'http://www.sitepoint.com/'
*/
This appears a little complicated but remember that in all destructuring assignments:
There are a number of other caveats. First, you can’t start a statement with a curly brace, because it looks like a code block, e.g.
// THIS FAILS
{ a, b, c } = myObject;
You must either declare the variables, e.g.
// THIS WORKS
const { a, b, c } = myObject;
or use parentheses if variables are already declared, e.g.
// THIS WORKS
({ a, b, c } = myObject);
You should also be wary of mixing declared and undeclared variables, e.g.
// THIS FAILS
let a;
let { a, b, c } = myObject;
// THIS WORKS
let a, b, c;
({ a, b, c } = myObject);
That’s the basics of destructuring. So when would it be useful? I’m glad you asked …
Continue reading %ES6 in Action: Destructuring Assignment%
Pressure is a JavaScript library for handling both Force Touch and 3D Touch on the web, bundled under one library with a simple API that makes working with them painless.
The post Pressure.js : JavaScript library for handling Force Touch & 3D Touch appeared first on Best jQuery.