JavaScript Static Methods

Static class methods are defined on the class itself.

You cannot call a static method on an object, only on an object class.

Example

class Car {
  constructor(name) {
    this.name = name;
  }
  static hello() {
    return "Hello!!";
  }
}

const myCar = new Car("Ford");

// You can call 'hello()' on the Car Class:
document.getElementById("demo").innerHTML = Car.hello();

// But NOT on a Car Object:
// document.getElementById("demo").innerHTML = myCar.hello();
// this will raise an error.

Continue reading JavaScript Static Methods

JavaScript Class Inheritance

Class Inheritance

To create a class inheritance, use the extends keyword.

A class created with a class inheritance inherits all the methods from another class :

Example

Create a class named “Model” which will inherit the methods from the “Car” class :

class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'I have a ' + this.carname;
  }
}

class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is a ' + this.model;
  }
}

let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = myCar.show();

 

The super() method refers to the parent class. Continue reading JavaScript Class Inheritance

JavaScript Classes

ECMAScript 2015, also known as ES6, introduced JavaScript Classes.

JavaScript Classes are templates for JavaScript Objects.

JavaScript Class Syntax

Use the keyword class to create a class.

Always add a method named constructor():

Syntax

class ClassName {
  constructor() { ... }
}

Example

class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
}

The example above creates a class named “Car”. Continue reading JavaScript Classes

JavaScript Closures

JavaScript variables can belong to:

The local scope or The global scope

Global variables can be made local (private) with closures.

Closures makes it possible for a function to have “private” variables.

Local Variables

A local variable is a “private” variable defined inside a function.

A function can access all variables in the local scope. Continue reading JavaScript Closures

JavaScript Function bind()

Function Borrowing

With the bind() method, an object can borrow a method from another object.

The example below creates 2 objects (person and member).

The member object borrows the fullname method from the person object :

Example

const person = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}

const member = {
  firstName:"Hege",
  lastName: "Nilsen",
}

let fullName = person.fullName.bind(member);

Continue reading JavaScript Function bind()

JavaScript Function apply()

Method Reuse

With the apply() method, you can write a method that can be used on different objects.

The JavaScript apply() Method

The apply() method is similar to the call() method (previous chapter).

In this example the fullName method of person is applied on person1 :

Example

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}

const person1 = {
  firstName: "Mary",
  lastName: "Doe"
}

// This will return "Mary Doe":
person.fullName.apply(person1);

Continue reading JavaScript Function apply()

JavaScript Function call()

Method Reuse

With the call() method, you can write a method that can be used on different objects.

All Functions are Methods

In JavaScript all functions are object methods.

If a function is not a method of a JavaScript object, it is a function of the global object (see previous chapter).

The example below creates an object with 3 properties, firstName, lastName, fullName. Continue reading JavaScript Function call()

JavaScript Function Invocation

The code inside a JavaScript function will execute when “something” invokes it.

Invoking a JavaScript Function

The code inside a function is not executed when the function is defined.

The code inside a function is executed when the function is invoked.

It is common to use the term “call a function” instead of “invoke a function“.

It is also common to say “call upon a function”, “start a function”, or “execute a function”.

In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called. Continue reading JavaScript Function Invocation

JavaScript Function Parameters

A JavaScript function does not perform any checking on parameter values (arguments).

Function Parameters and Arguments

Earlier in this tutorial, you learned that functions can have parameters:

function functionName(parameter1, parameter2, parameter3) {
  // code to be executed
}

Function parameters are the names listed in the function definition.

Function arguments are the real values passed to (and received by) the function. Continue reading JavaScript Function Parameters

JavaScript Function Definitions

JavaScript functions are defined with the function keyword.

You can use a function declaration or a function expression.

Function Declarations

Earlier in this tutorial, you learned that functions are declared with the following syntax:

function functionName(parameters) {
  // code to be executed
}

Declared functions are not executed immediately. They are “saved for later use”, and will be executed later, when they are invoked (called upon). Continue reading JavaScript Function Definitions