JavaScript is partly an object-oriented language.
To learn JavaScript, we got to learn the object-oriented parts of JavaScript.
In this article, we’ll look at functions and objects.
Iterators can be created with generator functions in JavaScript.
For instance, we can write:
function* genFn() {
yield 1;
yield 2;
yield 3;
}
We have a generator function as indicated by the function*
keyword.
Then we can call it to return an iterator:
const gen = genFn();
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());
We called genFn
to get an iterator.
Then we called next
on the returned iterator.
And the next
method will return the object with the value
and done
properties.
value
has the value from yield
.
And done
is a boolean to indicate whether all the values are returned.
We don’t need IIFEs for containing variables in blocks anymore.
We can use let
and const
to declare block-scoped variables.
So if we have:
(function() {
var foo = 0;
}());
console.log(foo);
Then the console log will throw the ‘Uncaught ReferenceError: foo is not defined’ error.
We can do the same thing with let
or const
variables in blocks”
{
let foo = 1;
const bar = 2;
}
Arrow functions are great for creating callbacks.
#programming #technology #web-development #javascript #software-development