shireen-bean / js-functions-ins-and-outs

JavaScript Function Arguments and Return Values.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

General Assembly Logo

JavaScript Functions - "Ins & Outs"

Prerequisites

Required Reading

JavaScript Functions - "Ins & Outs" - Study

Introduction

JavaScript function argument and return values

Objectives

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

"Ins & Outs"

"Ins"

Zero or more arguments

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.

Demo - arguments
const product = function product() {

};
Code along - arguments
const max = function max() {

};

Could we accomplish something similar using a single argument?

Lab - single array argument

Write functions that take an array and return a product or max.

Reference types as arguments

Reference types passed as arguments can be modified within the functions.

Demo - reference type arguments
const arrayTimes2 = function arrayTimes2() {

};
Code along - reference type arguments
const addProperty = function addProperty() {

};

Functions are valid arguments.

const transform = function transform(value, predicate, mutator) {

};
Lab - reference type arguments

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() {

};

"Outs"

Reference types as returns values

Reference type literals returned from functions create new instances of the type specified.

Demo - return new arrays
const createArray = function createArray() {

};
Code along - return new objects
const createPerson = function createPerson(givenName, surname, bornOn, height, weight, eyeColor) {

};

Functions as returns values

Functions returned from functions generate a closure. Closures provide great utility.

Demo - return new functions
const memoFactory = function memoFactory() {

};
Code along - return new functions

Functions returned from functions generate a closure. Closures provide great utility.

const counterFactory = function counterFactory() {

};

Additional Resources

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

About

JavaScript Function Arguments and Return Values.

License:Other


Languages

Language:JavaScript 100.0%