The identifier this

This mechanism is a different concept than in the object-oriented languages. In JavaScript, if a function contains a this reference inside it, then that reference usually points to an object. But which object it points to, usually depends on how the function was called.

It is important to realize that this does not refer to the function itself, as is the most common misconception.

What this references depends on how the function in question was called. There are 4 possibilities of what this references depending on the context.

Check variable type

Type typeof <variable_name>.

Prototypes

The prototype mechanism in JavaScript is complicated, but it heavily bases on the concept of linkages. Linkages tend to be used - or abused - in the class-inheritant fashion known from C++.

When we reference a property on an object, if that property does not exist, JavaScript will automatically use that object's internal prototype reference to find another object to look for the property on. We could think of this almost as a fallback if the property is missing.

Tutorial is based mainly on the book "You Don't Know JS" [1].

Dependency injection

This means accepting an object from the outside instead of defining it inside.

Logical expressions can return data

When one of the operands is not of logical type, logical expressions involving it can return a non-boolean value. Consider such an example:

var a = "a";
var b = "b";
a || b; // "a"
!a || b; // "b"

Man, this is awesome!

Callback functions

A callback function is a function passed to the function for further execution, vide here. Example is as follows:

function doItWhenReady() {
    alert('Hello from callback!');
}

function process(callback) {
    if (typeof callback !== 'function') {
         callback = false;
    }

    if (callback) {
         callback();
    }
}

process(doItWhenReady);

Such callback implementation allows for design based on Petri nets in JavaScript.

What are the callback function arguments? Where do they come from?

Ryan McDermott noticed:

Functions should do one thing:
This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you'll be ahead of many developers.

See here.

.bind() function is a recommended way to pass arguments to a callback, see here.
In what is bind() different from apply()? "Call and apply" call a function while bind creates a function.", says user Nope of SO here.

Promises

A promise represents the eventual result of an asynchronous operation ^1.

Why "ECMA"

ECMA stands for the European Computer Manufacturers Association. The institution has been renamed on the way and its current name is European Association for Standardizing Information and Communication Systems, see association history.

Function scope

Every time a function is called, a new variable scope is created.

Variables declared in the parent scope are available to that function.

Variables declared within the new scope are not available when the function exits.

This was described by Java2S[2].


  1. YDKJS ↩︎

  2. Java2S ↩︎