Giter VIP home page Giter VIP logo

js-functions-ins-and-outs's Introduction

General Assembly Logo

JavaScript Functions - "Ins & Outs"

Prerequisites

Required Reading

JavaScript Functions - "Ins & Outs" - Study

Preparation

  1. Fork and clone this repository.

  2. Create a new branch, training, for your work.

  3. Install dependencies with npm install.

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 reference types as arguments
  • Create and invoke functions that return reference types
  • 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() {
  let result = 1;

  for (let i = 0; i < arguments.length; i++) {
    result = result * arguments[i];
  }

  return result;
};
Code along - arguments
// takes an arbitrary number of arguments (each arg
// should be a number), finds the largest one, and
// returns that member

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
// Write a function that takes an array full of integers, doubles each value, and
// returns a new array with those values.
const arrayTimes2 = function arrayTimes2() {
  let result = [];

  for (let i = 0; i < arguments[0].length; i++) {
    result[i] = arguments[0][i] * 2;
  }

  return result;
};
Code along - reference type arguments
const addProperty = function addProperty(obj, prop, val) {
  // this function takes an object and adds a property
  // to it

};

Functions are valid arguments.

const transform = function (values, predicate, mutator) {
  // if the predicate is true, mutate the value, otherwise don't mutate it

};

"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() {
  let result = [];

  for (let i = 0; i < arguments.length; i++) {
    result[i] = arguments[i];
  }

  return result;
};
Lab - reference types as arguments and return values

Write a function that takes an array, a predicate, and a mutator. It should create a new array to hold all of either an existing element in the array or the transformed value for that element 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(array, predicate, mutator) {
  // if the predicate is true, mutate the value, otherwise don't mutate it

};
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 (rememberMe) {
  let memo = rememberMe;

  return function () {
    return memo;
  };
};
Code along - return new functions

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

const counterFactory = function(count) {

};

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

js-functions-ins-and-outs's People

Contributors

jrhorn424 avatar payne-chris-r avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.