Bogumil Wrona

IT Consultant | R&D | [...] Solutions

JavaScript Programming Paradigms

Image from Wikipedia

programming paradigm

A programming paradigm is a fundamental style of computer programming, a way of building the structure and elements of computer programs. Capablities and styles of various programming languages are defined by their supported programming paradigms; some programming languages are designed to follow only one paradigm, while others support multiple paradigms.

Read more

In JavaScript we can find multiple paradigms.

Imperative Programming

That type of programming is based on describing actions.

It is about thinking about algorithm which solves the problem and implementation of it, nothing more.

Prototype-based object-oriented programming

That type of programming is based on prototypical objects.

Functional / Metaprogramming

That type of programming is based on manipulation of execution model.

Examples

Imperative Programming
1
2
3
4
5
6
7
8
9
var numbers = [1, 2, 3, 4, 6, 10],
    tripled = [];

for(var i = 0; i < numbers.length; i++) {
    var newNum = numbers[i] * 3;
    tripled.push(newNum);
};

console.log(tripled); //=> [3, 6, 9, 12, 18, 30]
Prototype-based object-oriented programming
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function Point(x, y) {
  this.x = x;
  this.y = y;
};

// creating new instance of Point:
new Point(0, 10);  //=> {x: 0, y: 10}

function Point3D(x, y, z) {
  Point.call(this, x, y);
  this.z = z;
};

// creating new instance of Point3D:
new Point3D(0, 10, 20);  //=> {x: 0, y: 10, z: 20}
Functional Programming / Metaprogramming
1
['A', 'B', 'C'].forEach(alert);

More about functional programming

Comments