JS 3 Array methods, Objects, Instances, prototypes, Object constructor and extends

Filter(), map(), find() and forEach() on arrays

Some methods implement arrow callback functions in their method call and return true/false if the conditions apply, they are called Predicate.

Some methods are Pure, which means they return a new array without modifying the original.

Includes() return true/false for value existence in the array:

//we can apply it directly on arrays, like most Predicate

[1,2,3,4,5].includes( 2 )        //true

Map() will return a new array applying its callback function, it's pure:

let namesArray = ['elamin', 'antigoni', 'chris']
namesArray.sort().map( (x) => x.toUpperCase() )  //['ANTIGONI', 'CHRIS', 'ELAMIN',]

//or we can reference an external function (without calling it using () )
function upper(x){
    return x.toUpperCase()
}
namesArray.map(upper)

.forEach() won't return an array, but a looped list of returns:

You can use forEach() after a map()/filter() but not the opposite:

.filter() will return an array with the elements that satisfy the callback function condition, is pure:

To filter(e) falsy values and arrays remember always to put e== true as the first condition:

.find() will return the first element matching:

findindex() works as a find() but is focused on indexes:

Map() and Filter() have different returns:

Some() run tests on each element of an array, returning true/false if at least one satisfies the callback function :

Every() check if all values of the array satisfy the callback function:

Reduce() returns a single value, it calls its reducer function on an accumulated initialValue. Used in Function Programming, doesn't modify the array, it returns a new one.

For a practical ReactJs example on Reduce() check Parallax.

Reverse() modifies the original array, it is pure.

Flat() creates a new array with all sub-array elements concatenated a, it is pur the same level:

FlatMap() returns a new array by applying the callback function to each element and flattens it:

To loop through arrays we use for() and while().

Objects, methods, and keys

Objects are variables that contain a collection of named values, stored in property: value pairs:

We can create objects out of variables as property:values pairs:

Objects can be edited by assigning a new property:value and use [variables] to dynamically access property values::

Or we could use Object.assign() to change an object property:

How we handle undefined/null results in objects:

We can also use instanceof to check if empty or null objects are still objects:

Primitive Data types can be objects with or without coercion, being Javascript a weakly typed language:

With different types of data, we can use Object.keys() iterable on them:

Object Methods are functions stored in the object that can use .this for properties in the object:

Property functions can be shortened:

We use Object.keys()/values()/entries() to access an object data or a for...of loop.

Objects methods can edit and return their own properties:

Object Instances and Prototype inheritance

Javascript doesn't have Static types or Static dispatching, everything is an instance(object) or a function(constructor), and even functions are instances of a function constructor.

Every object has a built-in private Object property [[prototype]], that can be swapped or edited:

The __proto__ property and Object.setPrototypeOf(to, from) allow objects to inherit [prototype]:

We set up __proto__ with the .prototype method:

Instances are new objects created from a constructor class functions:

Instance object with prototype

Objects.create() inherit the parent object as __proto__ while instances inherit the constructor [prototype] as __proto__.

Javascript uses prototypical objects as Templates from which new Objects inherit properties and methods (states and behaviors) in their [[prototype]].

Arrays and RegEx, for example, come with [[prototype]] included :

To access the [[prototype]] methods and properties:

An array object __proto__ (a constructor function)

All Javascript Objects inherit [[prototype]], a property/function that contains all the properties and methods available to the object.

Any method/property in the prototypical object constructor function body can be added to the prototype, which is more memory efficient and allows for object-specific syntax:

The new method is in the [[prototype]]

We use .hasOwnProperty() on Instances to check the constructor function properties:

This values returned from prototype.methods will become instance properties,

Instance prototype returned new This instance

We can use Object.assign to more easily add properties and methods to prototypes:

A constructor function can inherit another constructor, its instances will have access to both:

one level inherit constructor

.Call() is a Javascript method, it invokes (call) a method from another object using this.owner object as an argument.

instanceof checks if a prototype chain of an instance has a constructor.prototype, returning a boolean value:

We can use instanceof for other types of data too:

Object constructor and extends

Functions are callable objects, so they can contain properties and methods:

We use Constructions functions as templates to create new objects using the new keyword:

We can use extends to extend function constructors, super() calls the previous constructor to include all its properties:

Extends also extends the [[prototype]] chain:

The Class keyword was introduced in 2015, it doesn't change Javascript's prototype-based nature but helps to make syntax more in line with C++ and Java.

It creates construction Objects templates whose properties and methods can be inherited by other objects, called instances:

Instances can modify the inherited [prototype] into new properties:

We can also use get() for methods and more super:

Last updated