SlideShare a Scribd company logo
JavaScript Functions We’ll Cover syntax arguments context closures Goal : you  <3  JavaScript Functions You.learn() Jupiter IT
Why? Functions are first class Closures are powerful Building block of JS knowledge Jupiter IT
The Basics var   square   =   function ( x ){ return   x * x ; } square(5) Function reference Function keyword Arguments Code body Return value Jupiter IT
3 Ways to Declare a Function var   f   =   function (){ alert ( 'hi' )} function   f (){ alert ( 'hi' )} window . f   =   function (){ alert ( 'hi' )} Jupiter IT
Everything is an Object I ask &quot;girl can I have closures, canvas, advanced features for my project?&quot; You say &quot;sure, but keep it simple, everything is an object&quot; I say &quot;JavaScript you've come a long way from copy-paste hacks&quot; You blush &quot;I know, but please, call me AJAX&quot;  -- Digital Crush Jupiter IT var   f   =   function ( a , b ){ return   a - b }; var   g   = “hi”; var   h   = 1;
Functions are First Class Argument Variable Return var   f   =   function ( a , b ){ return   a - b }; [1,2,3,4].sort ( f ); var   g   =   function (){ return   true ;} function   bark (){ return   function (){ alert ( 'bark' ); } } Jupiter IT
Functions are First Class Object attribute Array element var   obj   =   { func :   function (){ alert ( 'i am a function' ); } } obj.func(); var   a   =   []; a [ 0 ]   =   function (){ return   true ;} a[0](); Jupiter IT
Passing Arguments Pass by value Pass by reference function   change ( obj ){ obj . a = obj . val + 1 ; } var   obj   =   { a :   1 }; change ( obj ); alert ( obj . a ); function   change ( x ){ x = x + 1 ; } var   a   =   1 ; change ( a ); alert ( a );   Jupiter IT // alerts 1 // alerts 2
Context this keyword function   f (){ this . barkCount ++; } f();   var   dog   =   { barkCount :   0 , bark :   function (){ this . barkCount ++; } } dog . bark (); Jupiter IT
Context Apply & Call Invoke as if function is a method of any object Pass in an array of arguments doYourBusiness . call ( dog ,   1 ,   2 ); doYourBusiness . apply ( dog ,   [ 1 , 2 ]); dog . doYourBusiness ( 1 ,   2 ); Jupiter IT
Arguments Overloading functions function   sum (){ var   s   =   0 ; for ( var   i = 0 ;   i < arguments . length ;   i ++){ s += arguments [ i ]; } return   s ; } alert ( sum ( 1 , 2 , 3 )); Jupiter IT // alerts 6
Arguments Argument length function   f ( a , b ){ if ( arguments . length   !=   f . length ) alert ( 'wrong nbr of args' ); else alert(‘right nbr of args’); } f ( 1 ); f ( 1,2 ); Jupiter IT // alerts “wrong nbr of args” // alerts “right nbr of args”
Closures Functions can access the variables and functions declared in its scope var   a = 1 ; function   f ( arg ){ alert ( a ); var   b = 1 ; function   g (){ alert ( a + b ); } g(); } Jupiter IT
Closures Private variables var   Dog   =   function (){ var   breed ; this . getBreed   =   function (){ return   breed ; } this . setBreed   =   function ( newBreed ){ breed   =   newBreed ; } } var   dog   =   new   Dog (); dog . setBreed ( 'doberman' ); alert ( dog . getBreed ()); dog.breed Jupiter IT
Closures Callbacks Todo   =   { markAsCompleted :   function ( task_name ){ MVC . Ajax ( '/complete/' + task_name ,   { onSuccess :   function ( transport ){ alert ( task_name ); } }) } } Jupiter IT
Closures (function(){…})() Don’t pollute global namespace ( function (){ var   count = 0 ; $ ( 'some_div' )[ 0 ]. click ( function (){ count   =   count + 1 ; alert ( count ); }) })(); Jupiter IT
Closures Most Common Misunderstanding Closures store reference to variable, not a copy var   a   =   {}; for ( var   i = 0 ;   i < 3 ;   i ++){ a [ i ]   =   function (){ alert ( i )}; } a [ 0 ](); a [ 1 ](); a [ 2 ](); Jupiter IT // alerts 2 // alerts 2 // alerts 2
Closures Most Common Misunderstanding Solution is another closure that “captures” state var   a   =   {}; for ( var   i = 0 ;   i < 3 ;   i ++){ ( function ( j ){ a [ j ]   =   function (){ alert ( j )}; })( i ) } a [ 0 ](); a [ 1 ](); a [ 2 ](); Jupiter IT // alerts 0 // alerts 1 // alerts 2
Two Things to Remember You can treat a function like any other object Closures are a way to use outside variables inside a function Jupiter IT Poppin' bottles with Document Object Models, So many lonely nights with Prototype. Opera, IE, Firefox, or Chrome, I write cross browser code and give you a home.  -- Digital Crush

More Related Content

PPTX
LinkedIn TBC JavaScript 100: Functions
PPTX
Javascript Function
PDF
JavaScript Functions
PPTX
Java script
PPTX
Javascript function
PPT
Unit 6 pointers
PDF
Currying and Partial Function Application (PFA)
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
LinkedIn TBC JavaScript 100: Functions
Javascript Function
JavaScript Functions
Java script
Javascript function
Unit 6 pointers
Currying and Partial Function Application (PFA)
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM

What's hot (20)

PPT
C++ Function
PDF
OOP and FP - Become a Better Programmer
PDF
Creating Lazy stream in CSharp
PDF
03 function overloading
PDF
From object oriented to functional domain modeling
PPTX
Functional Programming in Javascript - IL Tech Talks week
PDF
Being functional in PHP (DPC 2016)
PDF
Functional Programming with JavaScript
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PDF
Functional Programming in JavaScript
PDF
FP in Java - Project Lambda and beyond
PDF
Extend GraphQL with directives
PPT
Function overloading(C++)
PPTX
Functions in C++
PDF
Java 8 Workshop
PPTX
Function C++
PPTX
Functional programming in JavaScript
ODP
Clojure basics
PPTX
Java 7, 8 & 9 - Moving the language forward
PDF
ECMAScript 6 Review
C++ Function
OOP and FP - Become a Better Programmer
Creating Lazy stream in CSharp
03 function overloading
From object oriented to functional domain modeling
Functional Programming in Javascript - IL Tech Talks week
Being functional in PHP (DPC 2016)
Functional Programming with JavaScript
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Functional Programming in JavaScript
FP in Java - Project Lambda and beyond
Extend GraphQL with directives
Function overloading(C++)
Functions in C++
Java 8 Workshop
Function C++
Functional programming in JavaScript
Clojure basics
Java 7, 8 & 9 - Moving the language forward
ECMAScript 6 Review
Ad

Viewers also liked (8)

PPT
JavaScript Functions
PPTX
JavaScript Conditional Statements
PDF
Writing MySQL User-defined Functions in JavaScript
PPT
JavaScript Control Statements I
PDF
Loops in JavaScript
PPTX
Javascript conditional statements
PPTX
Loops in java script
PDF
Functional Javascript
JavaScript Functions
JavaScript Conditional Statements
Writing MySQL User-defined Functions in JavaScript
JavaScript Control Statements I
Loops in JavaScript
Javascript conditional statements
Loops in java script
Functional Javascript
Ad

Similar to JavaScript Functions (20)

PDF
JavaScript Primer
PPTX
Awesomeness of JavaScript…almost
PDF
Rediscovering JavaScript: The Language Behind The Libraries
PDF
The mighty js_function
PDF
java script functions, classes
PDF
Javascript development done right
PPT
JavaScript: The Language
PPTX
Object oriented java script
PPTX
The JavaScript Programming Language
PPT
Wakanday JS201 Best Practices
PPT
Object Oriented JavaScript
PPT
Intermediate JavaScript
PDF
Advanced Object-Oriented JavaScript
PDF
Js in-ten-minutes
PPT
Basic Javascript
PDF
Object-Oriented JavaScript
PPT
JavaScript - Programming Languages course
PDF
JavaScript: Patterns, Part 1
PPTX
Front end fundamentals session 1: javascript core
PDF
Closures in Javascript
JavaScript Primer
Awesomeness of JavaScript…almost
Rediscovering JavaScript: The Language Behind The Libraries
The mighty js_function
java script functions, classes
Javascript development done right
JavaScript: The Language
Object oriented java script
The JavaScript Programming Language
Wakanday JS201 Best Practices
Object Oriented JavaScript
Intermediate JavaScript
Advanced Object-Oriented JavaScript
Js in-ten-minutes
Basic Javascript
Object-Oriented JavaScript
JavaScript - Programming Languages course
JavaScript: Patterns, Part 1
Front end fundamentals session 1: javascript core
Closures in Javascript

More from Brian Moschel (12)

KEY
A Very Biased Comparison of MVC Libraries
PPTX
FuncUnit
PDF
Bottom Up
PDF
Headless Js Testing
PPTX
Comet: an Overview and a New Solution Called Jabbify
PDF
Web 2.0 Expo Notes
PDF
Comet, Simplified, with Jabbify Comet Service
PDF
Building an App with jQuery and JAXER
ODP
ODP
Basic inheritance in JavaScript
PDF
Things to avoid in JavaScript
PDF
Javascript and DOM
A Very Biased Comparison of MVC Libraries
FuncUnit
Bottom Up
Headless Js Testing
Comet: an Overview and a New Solution Called Jabbify
Web 2.0 Expo Notes
Comet, Simplified, with Jabbify Comet Service
Building an App with jQuery and JAXER
Basic inheritance in JavaScript
Things to avoid in JavaScript
Javascript and DOM

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm

JavaScript Functions

  • 1. JavaScript Functions We’ll Cover syntax arguments context closures Goal : you <3 JavaScript Functions You.learn() Jupiter IT
  • 2. Why? Functions are first class Closures are powerful Building block of JS knowledge Jupiter IT
  • 3. The Basics var square = function ( x ){ return x * x ; } square(5) Function reference Function keyword Arguments Code body Return value Jupiter IT
  • 4. 3 Ways to Declare a Function var f = function (){ alert ( 'hi' )} function f (){ alert ( 'hi' )} window . f = function (){ alert ( 'hi' )} Jupiter IT
  • 5. Everything is an Object I ask &quot;girl can I have closures, canvas, advanced features for my project?&quot; You say &quot;sure, but keep it simple, everything is an object&quot; I say &quot;JavaScript you've come a long way from copy-paste hacks&quot; You blush &quot;I know, but please, call me AJAX&quot; -- Digital Crush Jupiter IT var f = function ( a , b ){ return a - b }; var g = “hi”; var h = 1;
  • 6. Functions are First Class Argument Variable Return var f = function ( a , b ){ return a - b }; [1,2,3,4].sort ( f ); var g = function (){ return true ;} function bark (){ return function (){ alert ( 'bark' ); } } Jupiter IT
  • 7. Functions are First Class Object attribute Array element var obj = { func : function (){ alert ( 'i am a function' ); } } obj.func(); var a = []; a [ 0 ] = function (){ return true ;} a[0](); Jupiter IT
  • 8. Passing Arguments Pass by value Pass by reference function change ( obj ){ obj . a = obj . val + 1 ; } var obj = { a : 1 }; change ( obj ); alert ( obj . a ); function change ( x ){ x = x + 1 ; } var a = 1 ; change ( a ); alert ( a ); Jupiter IT // alerts 1 // alerts 2
  • 9. Context this keyword function f (){ this . barkCount ++; } f(); var dog = { barkCount : 0 , bark : function (){ this . barkCount ++; } } dog . bark (); Jupiter IT
  • 10. Context Apply & Call Invoke as if function is a method of any object Pass in an array of arguments doYourBusiness . call ( dog , 1 , 2 ); doYourBusiness . apply ( dog , [ 1 , 2 ]); dog . doYourBusiness ( 1 , 2 ); Jupiter IT
  • 11. Arguments Overloading functions function sum (){ var s = 0 ; for ( var i = 0 ; i < arguments . length ; i ++){ s += arguments [ i ]; } return s ; } alert ( sum ( 1 , 2 , 3 )); Jupiter IT // alerts 6
  • 12. Arguments Argument length function f ( a , b ){ if ( arguments . length != f . length ) alert ( 'wrong nbr of args' ); else alert(‘right nbr of args’); } f ( 1 ); f ( 1,2 ); Jupiter IT // alerts “wrong nbr of args” // alerts “right nbr of args”
  • 13. Closures Functions can access the variables and functions declared in its scope var a = 1 ; function f ( arg ){ alert ( a ); var b = 1 ; function g (){ alert ( a + b ); } g(); } Jupiter IT
  • 14. Closures Private variables var Dog = function (){ var breed ; this . getBreed = function (){ return breed ; } this . setBreed = function ( newBreed ){ breed = newBreed ; } } var dog = new Dog (); dog . setBreed ( 'doberman' ); alert ( dog . getBreed ()); dog.breed Jupiter IT
  • 15. Closures Callbacks Todo = { markAsCompleted : function ( task_name ){ MVC . Ajax ( '/complete/' + task_name , { onSuccess : function ( transport ){ alert ( task_name ); } }) } } Jupiter IT
  • 16. Closures (function(){…})() Don’t pollute global namespace ( function (){ var count = 0 ; $ ( 'some_div' )[ 0 ]. click ( function (){ count = count + 1 ; alert ( count ); }) })(); Jupiter IT
  • 17. Closures Most Common Misunderstanding Closures store reference to variable, not a copy var a = {}; for ( var i = 0 ; i < 3 ; i ++){ a [ i ] = function (){ alert ( i )}; } a [ 0 ](); a [ 1 ](); a [ 2 ](); Jupiter IT // alerts 2 // alerts 2 // alerts 2
  • 18. Closures Most Common Misunderstanding Solution is another closure that “captures” state var a = {}; for ( var i = 0 ; i < 3 ; i ++){ ( function ( j ){ a [ j ] = function (){ alert ( j )}; })( i ) } a [ 0 ](); a [ 1 ](); a [ 2 ](); Jupiter IT // alerts 0 // alerts 1 // alerts 2
  • 19. Two Things to Remember You can treat a function like any other object Closures are a way to use outside variables inside a function Jupiter IT Poppin' bottles with Document Object Models, So many lonely nights with Prototype. Opera, IE, Firefox, or Chrome, I write cross browser code and give you a home. -- Digital Crush

Editor's Notes

  • #2: By the end: Understand complex JS code Be comfortable with passing functions around like its nothin Have a decent understanding of closures and how to use them Fully “Functional”