DEV Community 👩‍💻👨‍💻

Jennifer Tieu
Jennifer Tieu

Posted on

Dev Journal: JS Error Propagation, TLS Intro, and JS Method Invocation

In this blog series, I am hoping to build the habit of showing my work, documenting my learnings, and sharing my journey.

Each post will consist of daily snippets of what I've studied or practice for the day. The hope is I can compile some of the content to write a more in-depth article on it in the future.

With that said, cheers to day 1. If you're reading this, I hope you enjoy and thank you for taking the time to stop by.

Eloquent JavaScript by Marijn Haverbeke (Third Edition)

Error Propagation

Not all problems can be prevented by programmers. However, instead of allowing these errors to cause the program to crash, you should address it in some way. For example, taking a bad input and continue running the program or report to the user what went wrong and exit the program.

In situations where errors are common and the caller of the function should explicitly take them into account then returning a special value is a good way to indicate an error. There are two downsides to returning a special value.

One, what if the function can already return every possible value? It would be difficult to distinguish success vs failure and its recommended to explicitly differentiate the two by wrapping the result in a object.

function lastElement(array) {
    if (array.length === 0) {
       return {failed: true};
    } else {
       return {element: array[array.length - 1]};
    }
}
Enter fullscreen mode Exit fullscreen mode

The second issue with returning special values is that the code can be redundant or awkward. If a function is called multiple times and it has to check whether null. Then the response to finding null is to return null the callers of the function will need to check for the return null etc.

Higher Performance Browser Networking by Ilya Grigorik

TLS Introduction

The SSL protocol was originally developed at Netscape to enable secure ecommerce transactions across the Web to protect customer data and ensure safe transactions.

It is implemented at the application layer. When SSL is used correctly, a third-party observer can only infer certain information about the data (connection endpoints, type of encryption, approximate amount of data etc.), but cannot read read or modify any of the actual data.

When the SSL protocol was standardized by the IETF, it was renamed to Transport Layer Security (TLS). TLS 1.0 is effectively an upgrade to SSL 3.0.

The Odin Project: Full-Stack JavaScript

Objects and Object Constructor: this keyword Assignment Reading: Method Invocation

A method is a function stored in a property of an object.

const myObj = {
  // helloMethod is a method
  helloMethod: function() {
    return "Hello World";
  }
};
const message = myObject.helloMethod()
Enter fullscreen mode Exit fullscreen mode

Method invocation occurs when an expression in a form of property accessor evaluates to a function object, i.e. obj.Func().

Method invocation requires a property accessor form to call the function, obj.Funct() not Func().

this in a method invocation

this is the object that owns the method in a method invocation.

const calculate = {
  number: 0,
  increment() {
    console.log(this === calculate); // => true
    this.number += 1;
    return this.number;
  }
};
// method invocation. this is calculate
calculate.increment(); // => 1
calculate.increment(); // => 2
Enter fullscreen mode Exit fullscreen mode

This is also true when a JavaScript object inherits a method from its prototype. The context of the inherited object is still the object itself.

In ECMAScript 2015 class syntax, the method invocation context is also the instance itself.

Top comments (0)

Classic DEV Post from 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!