Paradigm and other types of JS variables
The way to access the object properties is as follows:
The “builder” defines a plane or a structure for each object to be created, and the plane is filled with the object properties.
Definition of a generic object
Following the same example, many times we want to create many instances of a same object. In this specific case, we want to make many instances of “Person”. For this we do the following:
“This” creates the rules inside the compiler, it indicates in which context a function is executed. In the previous example, we set that the new variables (“name”, “lastName”, “foreign”) belong to the context where the function was executed.
“This” is dynamically defined while the program is running, and its behavior may vary.
To define it, two methods can be used:
- .bind: The method .bind() creates a new function that, when invoked, assigns to its operator “this” the delivered value, with a sequence of given arguments. The use of .bind() is important because each function has its own given context, and it is necessary to specify it in an implicit way before calling a method that uses values associated with the “this”.
- apply / call: There exist two options for this method:
- apply: It allows us to call a function passing it through a context and a series of parameters in the form of an array.
- call: It allows us to call a function passing it through a context and a series of parameters individually.
The difference with .bind() is that .bind() returns a new function with an assigned context.
About classes and prototypes
Objects will always have a prototype assigned, which is the mother object from which they “inherit” their properties.
Let us see an example of a chain of prototyping that Dani showed us in this presentation:
In this stage we would be redefining the prototype that comes by default with the object “Person”. We then add the function “showName”. Thus, we achieve that by creating a new object “Person”, it inherits such function automatically from its corresponding prototype, as we can see here:
On a third level, the same happens with the prototype of “Pensioner”, which will inherit the attributes from “Employee” and “Person”.
Do not miss the next article about the meetup of Frontenders, where we will be telling you everything we learnt about React.