-
JavaScript Class Basic Syntax
Frequently it is necessary to create many objects of the same kind. As it was already noted in chapter Constructor, operator “new”, you can do it with the help of the new function. In modern JavaScript, there exists a more advanced “class” construct, introducing new features that are useful for object-oriented programming. The “class” Syntax The syntax…
-
JavaScript Prototype Methods, Objects Without __proto__
Let’s first explore the modern methods for setting up a prototype. Amongst them are: So, you can use the mentioned methods instead of the __proto__. Take a look at the following example: There is an alternative argument for Object.create: property descriptors. You have the option of providing additional properties to the object there. For example: For performing…
-
JavaScript Native Prototypes
The “prototype” property is commonly used by the core of JavaScript. It is used by all the built-in constructor functions. Let’s start at the details. Object.prototype Imagine you output an object that is empty: Probably, you wonder where the code creating the string “[object Object]” is. It is the built-in toString method. But, in fact, the obj is empty. Note that short notation obj…
-
JavaScript F.prototype
JavaScript allows creating objects using a constructor function, like the new F(). Once F.prototype is an object, the new operator may use it for setting [[Prototype]] for the new object. Please, take into consideration that here F.prototype means an ordinary property, known as “prototype” on F. It may sound similar to the term “prototype.” Still, here it is intended an ordinary property with this name. For instance:…
-
JavaScript Prototypal inheritance
In programming, it is often necessary to take something and extend it. For example, there is a user object with its methods and properties. You intend to make admin and guest as pretty modified variants of it. So you wish to reuse what you have in the user, building a new object on top of it. The prototypal inheritance feature is there…
-
JavaScript Property Getters and Setters
In this chapter, we cover the types of object properties. As a rule, there exist two object property types: data properties and accessor properties. All the properties we represented before are data properties. Accessor properties are relatively new and execute on getting and setting values. However, they seem like a regular property to the external…
-
JavaScript Property Flags and Descriptors
Objects are capable of storing properties. Unlike a regular property, an object property is considered a more powerful and flexible thing. In this chapter, we are going to represent to you several additional options for configuration. Property Flags Object properties include three specific attributes (besides the value). They are called flags. They can be: writable – in…
-
JavaScript Arrow Functions Revisited
Arrow functions have both some useful and unique features and limitations that we are going to cover in this chapter. In JavaScript, you can meet a lot of cases when it’s necessary to write a small function, which is executed in another place. To be more precise, let’s consider several examples: arr.forEach(func) – for each array item func is executed by forEach. setTimeout(func)– the…
-
JavaScript Function Binding
In this chapter, we are going to represent the function binding: a method creating a new function that, when called, has its this keyword set to the provided value. Now, imagine that you pass object methods as callbacks, for example, to setTimeout, and there exists a known issue: losing this. Let’s see how to fix it. Losing “this” You…
-
JavaScript Decorators and Forwarding, Call / Apply
In JavaScript, you can work with functions with excellent flexibility. You can pass them around, use them as objects. In this chapter, we are going to show you how to decorate them and forward calls between them. Using “func.call” for the Context You can use a unique built-in function method func.call(context, …args), allowing to invoke a…
