Javascript Fundamentals: Functions

Jyoti gupta
3 min readApr 4, 2021

Why every fault condemned ere it be done: Mine were the very cipher of a function

— William Shakespeare, Measure for Measure

Functions are used to specify the behavior of objects. Generally, the craft of programming is the factoring of a set of requirements into a set of functions and data structures.

Function Literal :

Function objects are created with function literals:

// Create a variable called add and store a function

// in it that adds two numbers.

var add = function (a, b) {

return a + b;

}

Method Invocation Pattern :

When the function is stored as a property of an object. When the method is invoked, this is bound to that object.

var person = {
name: 'Calvin',
age: 25,
greet: function () {
alert('My name is ' + this.name + '.');
}
};
person.greet(); //My name is Calvin.

Function Invocation Pattern :

When a function is invoked with this pattern, this is bound to the global object. If the method defines a variable and assigns it the value of this, the inner function will have access to this through that variable. By convention, the name of that variable is that.

// Augment myObject with a double method.

myObject.double = function ( ) {
var that = this; // Workaround.

var helper = function ( ) {
that.value = add(that.value, that.value);
};

helper( ); // Invoke helper as a function.
};

// Invoke double as a method.

myObject.double( );
document.writeln(myObject.value); // 6

Constructor Invocation pattern :

JavaScript is a prototypal inheritance language. That means that objects can inherit properties directly from other objects. The language is class-free.

If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function's prototype member, and this will be bound to that new object.

The new prefix also changes the behavior of the return statement. We will see more about that next.

// Create a constructor function called Quo.
// It makes an object with a status property.

var Quo = function (string) {
this.status = string;
};

// Give all instances of Quo a public method
// called get_status.

Quo.prototype.get_status = function ( ) {
return this.status;
};

// Make an instance of Quo.

var myQuo = new Quo("confused");

document.writeln(myQuo.get_status( )); // confused

The Apply Invocation Pattern :

The apply method lets us construct an array of arguments to use to invoke a function. It also lets us choose the value of this. The apply method takes two parameters. The first is the value that should be bound to this. The second is an array of parameters.

// Make an array of 2 numbers and add them.

var array = [3, 4];
var sum = add.apply(null, array); // sum is 7

// Make an object with a status member.

var statusObject = {
status: 'A-OK'
};

// statusObject does not inherit from Quo.prototype,
// but we can invoke the get_status method on
// statusObject even though statusObject does not have
// a get_status method.

var status = Quo.prototype.get_status.apply(statusObject);
// status is 'A-OK'

Will continue with other blogs to cover inheritance prototyping, closure, callbacks, module, cascade, currying.

Hope that will help to gain a better understanding.

--

--