Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learn ECMAScript
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learn ECMAScript

Learn ECMAScript - Second Edition

By : MOHAN, Narayan Prusty
4 (1)
close
close
Learn ECMAScript

Learn ECMAScript

4 (1)
By: MOHAN, Narayan Prusty

Overview of this book

Learn ECMAScript explores implementation of the latest ECMAScript features to add to your developer toolbox, helping you to progress to an advanced level. Learn to add 1 to a variable andsafely access shared memory data within multiple threads to avoid race conditions. You’ll start the book by building on your existing knowledge of JavaScript, covering performing arithmetic operations, using arrow functions and dealing with closures. Next, you will grasp the most commonly used ECMAScript skills such as reflection, proxies, and classes. Furthermore, you’ll learn modularizing the JS code base, implementing JS on the web and how the modern HTML5 + JS APIs provide power to developers on the web. Finally, you will learn the deeper parts of the language, which include making JavaScript multithreaded with dedicated and shared web workers, memory management, shared memory, and atomics. It doesn’t end here; this book is 100% compatible with ES.Next. By the end of this book, you'll have fully mastered all the features of ECMAScript!
Table of Contents (14 chapters)
close
close

Arrow functions

An arrow function is, at first glance, just a fancy way to create regular JavaScript functions (however, there are some surprises). Using arrow functions, you can create concise one-liner functions that actually work!

The following example demonstrates how to create an arrow function:

let circumference = (pi, r) => {
let ans = 2 * pi * r;
return ans;
}
let result = circumference(3.141592, 3);
console.log(result); // Outputs 18.849552

Here, circumference is a variable, referencing to the anonymous arrow function.
The previous code is similar to the following code in ES5:

var circumference = function(pi, r) {
var area = 2 * pi * r;
return area;
}
var result = circumference(3.141592, 3);
console.log(result); //Output 18.849552

If your function contains just a single statement (and you want to return the result of that statement), then you don't have to use the {} brackets to wrap the code. This makes it a one-liner. The following example demonstrates this:

let circumference = (pi, r) => 2 * pi * r;
let result = circumference(3.141592, 3);
console.log(result); //Output 18.849552

When {} brackets are not used then the value of the statement in the body is automatically returned. The preceding code is equivalent to the following:

let circumference = function(pi, r) { return 2 * pi * r; }
let result = circumference(3.14, 3);
console.log(result); //Output 18.84

Also, if there's only a single argument, you can omit the brackets to make the code even shorter. Consider the following example:

let areaOfSquare = side => side * side;
let result = areaOfSquare(10);
console.log(result); //Output 100

Since there is only one argument, side, we can omit the circular brackets for this.

The value of "this" in an arrow function

In arrow functions, the value of the this keyword is the same as the value of the this keyword of the enclosing scope (the global or function scope, whichever the arrow function is defined inside). That means, instead of referring to the context object (that is, the object inside which the function is a property), which is the value of this in traditional functions, this instead refers to global or function scope, in which the function is called.
Consider this example to understand the difference between the traditional functions and the arrow functions, this value:

var car = {
name: 'Bugatti',
fuel: 0,
// site A
addFuel: function() {
// site B
setInterval(function() {
// site C
this.fuel++;
console.log("The fuel is now " + this.fuel);
}, 1000)
}
}


What do you think will happen when you call the car.addFuel() method? If you guessed The fuel is now undefined will appear forever, then you are right! But why?!

When you define the addFuel method inside the function() {} (above site B), your this keyword refers to the current object. However, once you go another level deeper into functions (site C), your this now points to that particular function and its prototypes. Hence, you cannot access the parent object's property with the this keyword.

How do we fix this? Take a look at these arrow functions!

var car = {
name: 'Bugatti',
fuel: 0,
// site A
addFuel: function() {
// site B
setInterval(() => { // notice!
// site C
this.fuel++;
console.log("The fuel is now " + this.fuel);
}, 1000)
}
}

Now, inside site C, the this keyword refers to the parent object. Hence, we're able to access the fuel property using the this keyword only.

Other differences between arrow and traditional functions

Arrow functions cannot be used as object constructors, that is, the new operator cannot be applied to them.
Apart from syntax, the value, and the new operator, everything else is the same between arrow and traditional functions, that is, they are both instances of the Function constructor.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learn ECMAScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon