|Written by Ian Elliot|
|Monday, 21 May 2018|
Page 2 of 3
Return and Parameters
You might well be thinking if the invocation operator evaluates the function it should return a result and this is the case. You can use the statement:
to specify what the function evaluates to.
If you don't specify a return result the function body returns undefined.
has the function body:
Now if you try:
you will see 3 displayed as this is the result of the function.
The value of the function can be any object not just a Number object (actually a primitive value) as in the example.
The final part of the Function object we need to examine is the use of parameters.
and now you can call the function using:
and you will see 3 displayed. The arguments 1 and 2 are automatically converted into the properties of the arguments object as arguments and arguments.
Notice that this means that when you define a function you can call it with as many arguments of any type you care to use.
The values that you specify when you call the function are still used to create the arguments object but they are also used to create the local variables a and b with a=arguments and b=arguments and hence the function will return the sum of a and b no matter what they are set to when the function is evaluated.
sets a to reference 1 and b to reference 2 and then evaluates the body.
Notice that function parameters can be any object and not just the Number objects used in the example. You can also think of what is passed as being a reference to an object, even it it happens to be a primitive value, in all cases.
You can also use as many parameters in a function definition as you care to define and use as many as you care to when you execute the function. For each parameter you give a value to there is a variable of the same name with that value as part of the function's code. Any missing parameters are undefined and it is up to your code to deal with this.
Other Ways Of Creating Function Objects
This said, there are times when converting a String object containing code into an executable Function object is useful, but these tend to be considered advanced techniques.
The purpose of the Function expression is to give you a way to define a function without using a String to store the code. This is more secure and it allows for optimizations. A Function expression also looks a lot like a lambda expression in other languages.
Notice that even though these look different they also create a Function object just like the constructor although with some differences.
Lets’ start with the Function expression.
The Function Expression
This is just a shortcut to creating a Function object and doesn't really introduce anything new but you can now write:
Notice that the function body is now specified as statements enclosed in curly brackets and it is specified as part of the program text not as a String.
For example the previous Function object can be written:
or by including line-breaks to produce a more usual formatting:
This has the advantage that this looks more like a function definition in a non-object-oriented language. In particular it also looks a lot more like a lambda expression which you will find in most modern languages.
It is important to keep in mind that while this looks like a simple function declaration as you would find in other languages it isn't.
A function expression creates a Function object exactly like the Function constructor does.
Also notice that the variable assigned to becomes a reference to the function object as with the constructor.
Also notice that again just as when you use the constructor the Function object is anonymous. All you can do is to set a variable to reference it but those variables can be set to reference something else at any time.
The Arrow Function ES2015
The arrow function is mostly a condensed syntax for the function expression. You can use it to define a function as simply as:
This returns a Function object which can be used in the normal way.
For example you can store a reference to the arrow function in a variable:
and you can call the function in the usual way:
There are some syntactic shortcuts you can use, but this is more or less all there is to an arrow function – with one exception. Arrow functions do not have a this of their own, they inherit whatever value for this is in use at the time of their declaration.
This makes them unsuitable for use as method definitions but much better for passing to other Functions as parameters.
Apart from this everything that is true of a function expression is also true of an arrow function.
Now we need to look at the Function statement.
|Last Updated ( Monday, 21 May 2018 )|