Bogumil Wrona

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

JavaScript Tips and Tricks

this keyword

1
2
3
4
var func = function() { return this },
    obj = { name: 'A', aFun: func };

obj.aFun(); //=> some global object, probably Window

Testing Existance

Using the loose inequality operator !=, it is possible to destinguish between null, undefined and anything else:

1
2
3
4
5
6
7
8
9
10
11
12
13
function exist(arg) {
  return arg != null;
};

// tests:
console.log(exist(null));               //=> false
console.log(exist(undefined));          //=> false
console.log(exist({}.notThere));        //=> false
console.log(exist((function() {}())));  //=> false
console.log(exist(0));                  //=> true
console.log(exist(false));              //=> true
console.log(exist({}));                 //=> true
console.log(exist([]));                 //=> true

Testing Truthy

1
2
3
function isTruthy(arg) {
  return (arg !== false) && exist(arg);
};

Functional Programming in JavaScript

Image from Wikipedia

functional programming

In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional programming emphasizes functions that produce results that depend only on their inputs and not on the program stateā€”i.e. pure mathematical functions. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.

Read more
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// example 1:
['A', 'B', 'C'].forEach(alert);

// example 2:
var splat = function(fn) {
  return function(array) {
    return fn.apply(null, array);
  };
}, unsplat = function(fn) {
  return function() {
    return fn.call(null, [].slice.call(arguments, 0));
  };
};

var addArrayElements = splat(function(x, y) {
  return x + y;
}),
joinElements = unsplat(function(array) {
  return array.join(' ');
});

console.log(addArrayElements([1, 2]));

console.log(joinElements(1, 2));

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.