Functions
Functions
A function is an object that contains executable code.
In Javascript functions are first class, meaning that the can be:
Assigned to variables
Passed as parameters
Returned from other functions
Declaring
Function Declaration
Just like declaring vars with var, the function is always declared in the beginning of the main scope.
This is called
hoisting.
So you can 'call' this function before declaring it.
function a() {
...
}Function Expression
You attribute it to a variable.
const a = function() {
}Returning Functions
Functions that are returned from other functions can be instantly called with:
Parameters (Arguments)
You can access the list of arguments by accessing the default variable arguments.
Rest Parameter
Using Spread Operator, but must always be the last parameter declared.
this
An implicit variable that always references the object responsible for its invocation.
So, it does not matter if you declare a function in
global scopeor inside anobject, if you call the function inside the object,thiswill reference theobject, and if you call it inglobal scopeit will reference theglobal scope.
IMPORTANT:
thiscannot be accessed by functions declared asarrow functions.
area: () => { return this.x * this.y };
const area = () => { return this.x * this.y };
getter and setter
It is also possible to defined the getters and setters using .defineProperty().
getter
setter
When working with setter always use the function name different from the property name.
It is common practice to just use
_property.
call, apply, bind
Using .call() and .apply(), you can invoke a function passing this as parameter.
Using call
function.call(obj, ...args):
function: Is the function to be executed.obj: Is the object to give thethiscontext to the function.args: Are additional parameters passed to the function.
Using apply
function.call(obj, [...args]):
function: Is the function to be executed.obj: Is the object to give thethiscontext to the function.args: Are additional parameters passed to the function byArray.
Using bind
Allows encapsulating this inside the function, returning it.
new
You can create multiple objects that share the same structure with Constructor Functions.
So that you eliminate duplication and have more reusability.
By convention they are declared with Capital Letters.
IMPORTANT: Cannot use
arrow functionsto declareConstructor Functions.
Public properties (the ones that will be returned) will be accessed with
this.
By declaring the function inside, each person object will have a getAge() of different instance. Thus taking more memory.
To address this...
You declare the getAge() inside the Constructor Funtion prototype.
Every
functionhas a property prototype__proto__, linked to the object created bynewand only theConstructor Functionsuse them.[This
__proto__is NOT the same__proto__from objects heritage]
cannot access Person.prototype.getAge = () => {}this.
Now all the instances of Person are inheriting the getAge and sharing its code.
instanceof
With it, it is possible to check if a object was created by a specific Constructor Function, analysing all of its prototype chain.
Arrow Function
More simple and direct approuch for declaring functions.
Downside is that they dont have their own
thisandarguments, so they end up inheriting from the upper closure.
Last updated