JavaScript for Beginners workshop module 4 - HTML5 Developer Conference 2015

Embed Size (px)

Citation preview

M4 Functions

Pablo Farias Navarro@ZenvaTweetshttps://zenva.com

Variables

Arrays

Functions

Canvas

Conditionals

Loops

Events

OOP

Intro

Functions are present in many programming languages and they allow you to write blocks of code that you can execute in different places, so that you don't have to type the same code and repeat yourself.

We've been using functions without you noticing.

Function Declaration

LIVE CODING

Let's create a function that convers units, from meters to cm

Var c = m * 100;

Show scope as well, what happens if you try to access c outside.

Methods

LIVE CODING

We've seen how you can give different properties to objects such as numbers, strings, arrays

Objects can also have properties that are functions. This is called a method.

In JS, functions are what's called first class citizens, this means they can be passed as parameters and assigned to variables.

Example: add weapon

We don't give it a name, that's called an anonymous function

Show function expressions

Coding Challenge!