-
JavaScript Promise
In JavaScript, a promise is a unique object linking the “producing code” and the “consuming code”. In other words, it is a “subscription list”. The “producing code” takes the time it needs to produce the promised result, and the promise makes the result available to the overall subscribed code whenever it’s ready. But, JavaScript promises are more…
-
JavaScript Introduction:callbacks
Most of the JavaScript actions are asynchronous. To be more precise, you initiate them now, but they finish later. For example, actions like that may be scheduled by setTimeout. For a better understanding, let’s consider the following example: In the case above, you can see the loadScript(src) function loading a script with given src. You can also use the…
-
JavaScript Custom Errors, Extending Error
In the course of developing something, it is necessary to have own error classes for reflecting particular things that can go wrong. For errors in network operations, HttpError is necessary, for searching operations-NotFoundError, for database operations- DbError, and more. They should support basic error properties such as name, message, and stack. Also, they can include other properties. For example, HttpError objects can contain…
-
JavaScript Error handling, “try..catch”
Even the greatest programmers can have errors in the scripts. Generally, errors can occur as a result of mistakes, unexpected user input, and a thousand other reasons. But, hopefully, there exists a try..catch syntax construct, allowing to catch errors, so the script can do something more reasonable. The syntax of try…catch Two blocks are included in thetry..catch construct: try and catch:…
-
JavaScript Mixins
In JavaScript, it is possible to inherit only from one object. There can be a single JavaScript F.prototypetype]] for an object. A class can extend only one class. But, at times it can meet limiting. For example, you have a StreetSweeper class and a class Bike and intend to mix them and have StreetSweepingBicycle. In this case, you can use the concept, called…
-
JavaScript Class Checking: “instanceof”
The operator instanceof is targeted at checking whether an object belongs to a specific class. It also helps to take inheritance into account. Now, let’s use it to build a polymorphic function, which treats arguments differently depending on their type. The instanceof operator The following syntax is used for the instanceof operator: It may return true in case the object belongs…
-
JavaScript Extending Built-in Classes
Array, Map, and other built-in classes are extendable. In the example below, NumsArray inherits from the native Array: The most impressive thing is that built-in methods such as map, filter, and more – return new objects of literally the inherited type NumsArray. Their internal implementation applies the constructor property of the object for that. So, in the example mentioned above, it looks like this:…
-
JavaScript Private & Protected Properties & Methods
One of the most complicated issues throughout the history of JavaScript is privacy. In JavaScript, it’s not tricky to make functions and variables private. But it’s not possible to make object properties private. It can become a severe problem when it comes to managing the state of an instance. No private properties mean that each code with access to…
-
JavaScript Static Properties and Methods
The keyword static describes a static method for a class. So, it’s likely to assign a method to the class function and not to its “prototype”. Methods like this are generally known as static. In a class, they start with the static keyword, as follows: So, it’s the same as directly assigning it as a property: The class constructor Car is the value…
-
JavaScript Class inheritance
In this chapter, we will explore the class inheritance. It is a way for one class to extend another one, and allows creating new functionality on top of the existing. The “extend” Keyword Imagine that you have a class Car: Now, let’s say that you intend to create a new class MyCar. Since car are cars, the MyCar class…
