JavaScript functions




A key concept in JavaScript and probably the most significant attribute which separates JavaScript from other conventional programming languages is how it treats functions and function argument patterns.

The way JavaScript implements and treats function often creates a lot of confusion as to whether JavaScript is an Object-Oriented Programming (OOP) or Functional Language.

The truth is JavaScript is both, A Functional Object Oriented Programming Language (FOOP).

Due

In JavaScript there is no function polymorphism.  Essentially what this means is that you can’t write a function with the same name with a different parameter list and rely on the compiler/interpreter to choose the correct one depending on parameters defined at run time.

In fact in JavaScript has a number of different conventions and patterns for functions when compared against other conventional languages.

In this post I will explain some of the most discerning differences of JavaScript functions and function argument patterns. Essentially as an accompaniment to my JavaScript Functions and Variables, Types and Values.

 

Defining functions

Functions are defined using the function keyword, which can be used in a function definition expression or in a function declaration statement.

A function is a special type of object, with special properties such as constructor and call. You can also add addtional properties to a function object.

It is this capability that makes function a first class citizen, because they can be passed around as arguments into other functions.

Constructor functions

A constructor function is no different from any other function. A function is used as a constructor function when it is used after the new keyword.

Any function can be a constructor function. If you have several objects which share the same implementation, then place that logic inside a constructor function, and then use the constructor function to create those objects.

A constructor function will return an object. You can use this inside the function body to assign new properties to the object.

So if you want to make many objects with the property Human initialized to the value "Woman", then you can create a new constructor function Woman that encapsulates that logic.

nested functions

Functions may be nested within other functions

 

In JavaScript, a function may be called with any number of arguments, no matter how many of them are listed, and arguments which are not provided become undefined.

 

The following code snippet will help to explain

 

The console will display

Eloquent JavaScript: A Modern Introduction to Programming

Dives deep into the JavaScript language to show you how to write beautiful, effective code.

Buy Now

function arguments and parameters

function definitions do not specify an expected type for parameters and function invocations do not do any type checking on the argument values.

function invocations do not even check the number of arguments being passed. If  a function is invoked with fewer arguments than the declared parameters the additional parameters are set to the JavaScript undefined value .

The Arguments object

In each function is invoked with a psuedo-array called arguments.  The arguments object enables arguments to passed to the function to be retrieved by number rather than name.

 

Arguments is not a real array it is an Arguments object.

 
We could now rewrite our function above as

Which will now output

 



callee and caller properties

The arguments object defines a callee and caller properties. The callee property refers to the currently running function . Caller refers to the function that called the currently executing function.  The caller property gives access to the call stack, and the callee property is useful to all unnamed functions to call themselves recursively.

In ECMAScript 5 strict mode these properties are guaranteed to raise a TypeError if you try to read or write them.

Immediately Invoked Function Expression (IIFE)

The immediate function pattern is a syntax that enables you to execute a function as soon as it is defined.

Essentially this is just a function expression that executed right after it’s creation. The term immediate function is not defined in the ECMAScript standard, but is widely accepted by the JavaScript community as a means to describe this pattern.

There are 3 parts to this pattern

  • Define a function using a function expression
  • Add parenthesis at the end e.g. ()
  • Wrap the whole function in parenthesis

Wrapper Object

Wrapper Objects are a grey area in JavaScript!

functions like String, Number, Boolean , Function, when called with new create wrapper objects for the primitive types

String is a global function that creates a primitive string when passed in an argument; it will try to convert that argument into a string.

 

You can also call String as a constructor function

Auto Boxing

The constructor of both the primitive strings and the object are both the String function. It’s an interesting fact that you can call .constructor on the primitive string, when we’ve already covered that primitive types cannot have methods!

This is a process called autoboxing. When you try to call a property or method on certain primitive types, JavaScript will first convert it into a temporary wrapper object, and access the property / method on it, without affecting the original.

JavaScript won’t complain when you try assign a property to a primitive type, because the assignment is done on that temporary wrapper object, not the primitive type itself.

Summary

Everything in JavaScript can be treated as an object. However, caution should still be taken when dealing with wrapper objects.

Functions in JavaScript are special kind of object and have additional properties.



Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines. A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

π