The difference between each method is the initialization of this .
The this Keyword
Note: that this is a reserved keyword and you cannot modify the value of this.
In the previous chapters we have learned how to create functions.
The code in the function is executed after the function is called.
Called as a function
The default global object in HTML is the HTML page itself, so functions belong to HTML pages.
The page object in the browser is the browser window (window object). The above function will automatically become a function of the window object.
myFunction () is the same as window.myFunction ():
The Global Object
The value of this becomes a global object when the function is not called by its own object.
The global object in a web browser is the browser window (window object).
This instance returns the value of this as a window object:
Function is called as a method
The following example creates an object ( myObject ), the object has two properties ( firstName and lastName ), and a method (< strong> fullName ):
fullName method is a function. Functions belong to objects. myObject is the owner of the function.
Test the following! Modify the fullName method and return the this value: