Page 1 of 2
Buy Now: from your local Amazon
An Idiomatic Approach
In The Beginning Was The Object
Real World Objects
The Function Object
Extract - The Function Object
Extract - Function Object Self Reference
The Object Expression
Function Scope, Lifetime & Closure
Parameters, Returns and Destructuring
Extract - Parameters, and Destructuring
How Functions Become Methods
Extract - ES2015 Class and Extends
Inheritance and Type
The Search For Type ***NEW!
Buy Now: from your local Amazon
Also by Ian Elliot
Just jQuery: The Core UI
Just jQuery: Events, Async & AJAX
This has the effect of also removing any ideas of function signature i.e. the types of the parameters and hence of function overloading. If you are familiar with languages where both of these ideas are central to object oriented programming this will seem to be a big loss however what you have in return is the idea that a function can be written to deal with any objects that happen to be passed to it.
and use the function to add two integers or two decimal valued numbers or even two strings although in this case the result is concatenation rather than addition. What is more you can pass any two objects to the add function without generating any compile time error. For example:
There are no restrictions on what sort of object you can pass to a function as a parameter and no restriction on what the function can return.
The whole idea of typeless programming is taken up in later chapters but this is where it first makes itself felt.
The fact that parameters are objects is a powerful feature but, as mentioned in an earlier chapter, in fact the entire parameter list is an object.
Every function, apart from arrow functions, has a local arguments object which contains all of the parameter values passed to the function in something that looks like an Array. What is more the function declaration does not determine what can be passed to the function at the point of use.
That is function declarations do not need to specify any parameters at all and you can call any function with any list of arguments you care to use.
which will add any number of parameters you care to specify. Notice that the declaration does not specify anything at all, not event their number, about the parameters that the function expects. You can call it using:
or with any number of parameters of any type.
This is really all we need to write any function and indeed other languages struggle to provide this level of flexibility.
So why do we have a parameter list in function declarations?
unpacks the first three parameters you pass into a, b and c respectively. If you pass fewer than three then any parameters that do not have values are undefined. If you pass more than three then only the first three values are used in the local variables but of course all of the values are still available in the arguments object.
displays 0, 1 2 and changing arguments changes the associated parameters and vice versa.
The association between arguments and parameters is broken in strict mode. Any changes to arguments do not change the parameters and vice versa.
Passing An Array To arguments - Spread
If parameters are passed to functions in the arguments array you might be thinking if you can bypass the comma separated list of parameters and simply pass an Array that is used as arguments.?
You can but only if you call the array using either the apply method that every Function object has or you are prepared to use some new syntax introduced with ES2015.
This passes the args array to the function as the arguments object which is then unpacked into the usual parameters.
Using the ES2015 spread operator … you can write the above as:
The spread operator will unbundle any iterable object like an array and it works in function arguments, elements of array literals and object expressions where key value pairs are needed.