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 scope or inside an object, if you call the function inside the object, this will reference the object, and if you call it in global scope it will reference the global scope.

IMPORTANT: this cannot be accessed by functions declared as arrow 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 the this context 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 the this context to the function.

  • args: Are additional parameters passed to the function by Array.

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 functions to declare Constructor 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 function has a property prototype __proto__, linked to the object created by new and only the Constructor Functions use them.

    • [This __proto__ is NOT the same __proto__ from objects heritage]

Person.prototype.getAge = () => {} cannot access 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 this and arguments, so they end up inheriting from the upper closure.

Last updated