JavaScript Functions - "Ins & Outs" - Study
JavaScript function argument and return values
By the end of this lesson, students should be able to:
- Create and invoke functions that take an arbitrary number of arguments
- Create and invoke functions that take objects as arguments
- Create and invoke functions that return objects
- Create and invoke functions that take functions as arguments
- Create and invoke functions that return functions
JavaScript provides a mechanism to handle arguments not in the function
definition: the arguments
object.
This object is referred to as array like
and is available within any function.
We'll examine how this object is used by creating some seemingly parameterless
functions.
const product = function product() {
};
const max = function max() {
};
Could we accomplish something similar using a single argument?
Write functions that take an array and return a product or max.
Reference types passed as arguments can be modified within the functions.
const arrayTimes2 = function arrayTimes2() {
};
const addProperty = function addProperty() {
};
Functions are valid arguments.
const transform = function transform(value, predicate, mutator) {
};
Write a function that takes an array, a predicate, and a mutator. It should replace all elements of the array for which the predicate returns true. The replacement value should be the result of invoking the mutator on the existing element.
const arrayTransform = function arrayTransform() {
};
Reference type literals returned from functions create new instances of the type specified.
const createArray = function createArray() {
};
const createPerson = function createPerson(givenName, surname, bornOn, height, weight, eyeColor) {
};
Functions returned from functions generate a closure. Closures provide great utility.
const memoFactory = function memoFactory() {
};
Functions returned from functions generate a closure. Closures provide great utility.
const counterFactory = function counterFactory() {
};
Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.