What is doing?
We have created an object called myBook and successfully passed the required parameters (called arguments when we call or invoke a function, as opposed to parameters when we define).
Finally, we saved myBook and retrieve the object with its corresponding properties. In this way, each new book object, we create gets transmitted values that are his assigned
Access to properties with this .
Here we have defined a child variable called which owns an object with name and property age, and a function to introduce.
The function records an introductory sentence using the properties of the object, accessed with ce .wanted_property.
By default, when used in global scope, that is, not in a defined function or object , this will refer to the global object. In the browser, the window is the global object.
Anything you write "in the open‚", you can read it as if there was a window. written before. Let’s take a look at this code snippet:
First , we define a function called returnThis in global scope that returns the value of this. We therefore call by writing returnThis ().
Since it has global scope, we can see it as window.returnThis (), which return what’s left of the point.
the Gotcha of this and its three solutions
If a function is not directly related to an object, the this in the function will refer to the global object.
Eseguiamo questo nel browser e vediamo cosa otteniamo:
# 2 Solve this with arrow functions
The second solution probably uses one of the best features that come with ES6, which are the arrow functions .
Arrow functions represent a more concise syntax for write functions , and they search a higher level as part of the search for this, in the sense of the function in which it is contained
so, if we define our object like this .
< / figure>
Our object has an outer function which, when called, returns the inner regular function.
We then call regularFunction and return the innermost function of the arrow.
, when called, it returns the window object .
An arrow function searches only level 1 scope. Since the containing regularFunction is not directly related to our object, the window is returned. However,
If, we nest an arrow function inside an arrow function, in a regular function, will get the expected result, which is the actual container object.
the outside is also an arrow function, so look for another level. The normal external function is directly related to the call
object and, therefore, the object is what is returned as this.
# 3 Solve this with the bind method
Finally, the third solution uses the bind method. the binding method is used to explicitly declare what this need Gets the object passed an argument and associates it with this keyword of the function.
In this example, we link chained to the function that normally returns the window object this. Pass it the object we want to refer to as this as an argument and our problem is solved.
Phew! It was a great dive. Now you may be wondering , why all the hassle and confusion with this
Thanks to this, we can create a "project" object once and dynamically create objects with different property values ‚Äã‚Äãas we have seen in our example of book.
We can also create methods that expose and manipulate the properties of an object with this .wanted_property.