SlideShare a Scribd company logo
JavaScript for ABAP Programmers
Functions as 1st Class Citizens
Chris Whealy / The RIG
ABAP
Strongly typed
Syntax similar to COBOL
Block Scope
No equivalent concept
OO using class based inheritance
Imperative programming

JavaScript
Weakly typed
Syntax derived from Java
Lexical Scope
Functions are 1st class citizens
OO using referential inheritance
Imperative or Functional programming
1st Class Functions
Any programming language in which functions are treated as “1st class citizens” is said to implement
“1st class functions”.
So, what does that mean – functions have voting rights?

© 2013 SAP AG. All rights reserved.

3
1st Class Functions
Any programming language in which functions are treated as “1st class citizens” is said to implement
“1st class functions”.
So, what does that mean – functions have voting rights?
No, a 1st class citizen is any object that can be:
 Created or destroyed dynamically

© 2013 SAP AG. All rights reserved.

4
1st Class Functions
Any programming language in which functions are treated as “1st class citizens” is said to implement
“1st class functions”.
So, what does that mean – functions have voting rights?
No, a 1st class citizen is any object that can be:
 Created or destroyed dynamically
 Treated as data, meaning that it can be both:
 Passed as an argument to a function and
 Returned as the result of running a function

© 2013 SAP AG. All rights reserved.

5
1st Class Functions
Any programming language in which functions are treated as “1st class citizens” is said to implement
“1st class functions”.
So, what does that mean – functions have voting rights?
No, a 1st class citizen is any object that can be:
 Created or destroyed dynamically
 Treated as data, meaning that it can be both:
 Passed as an argument to a function and
 Returned as the result of running a function
In JavaScript, all the above statements are applicable to functions because a function is simply an
object “with an executable part”. This means a JavaScript function can be treated either as:
 An object like any other data object, or as
 A executable unit of code

© 2013 SAP AG. All rights reserved.

6
1st Class Functions: Dynamic Creation
A JavaScript function can be created dynamically based on the data available at runtime.
// List of animals and the noises they make
var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"];
// A function that returns a text string describing an animal noise
function noiseMaker(name) {
var n
= animalNoises.indexOf(name);
var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1];
return "A " + name + " says " + noise;
}

© 2013 SAP AG. All rights reserved.

7
1st Class Functions: Dynamic Creation
A JavaScript function can be created dynamically based on the data available at runtime.
// List of animals and the noises they make
var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"];
// A function that returns a text string describing an animal noise
function noiseMaker(name) {
var n
= animalNoises.indexOf(name);
var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1];
return "A " + name + " says " + noise;
}
// Dynamically create some function objects specific to certain animals
var pigSays
= new Function("alert("" + noiseMaker("pig") + "");");
var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");");

© 2013 SAP AG. All rights reserved.

8
1st Class Functions: Dynamic Creation
A JavaScript function can be created dynamically based on the data available at runtime.
// List of animals and the noises they make
var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"];
// A function that returns a text string describing an animal noise
function noiseMaker(name) {
var n
= animalNoises.indexOf(name);
var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1];
return "A " + name + " says " + noise;
}
// Dynamically create some function objects specific to certain animals
var pigSays
= new Function("alert("" + noiseMaker("pig") + "");");
var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");");
// Call these dynamically created function objects using the invocation operator
pigSays();

© 2013 SAP AG. All rights reserved.

9
1st Class Functions: Dynamic Creation
A JavaScript function can be created dynamically based on the data available at runtime.
// List of animals and the noises they make
var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"];
// A function that returns a text string describing an animal noise
function noiseMaker(name) {
var n
= animalNoises.indexOf(name);
var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1];
return "A " + name + " says " + noise;
}
// Dynamically create some function objects specific to certain animals
var pigSays
= new Function("alert("" + noiseMaker("pig") + "");");
var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");");
// Call these dynamically created function objects using the invocation operator
pigSays();
sheepSays();
© 2013 SAP AG. All rights reserved.

10
1st Class Functions: Dynamic Destruction
Setting a function object to null destroys that object.
// Destroy the dynamically created function objects
pigSays
= null;
sheepSays = null;

Notice here that reference is made to the function object itself, not the result of invoking that function.
In other words, the invocation operator () must not be used.

© 2013 SAP AG. All rights reserved.

11
1st Class Functions: Functions as Arguments
Since JavaScript treats a function is an object, the functions created in the previous example can be
passed as parameters in the same way you would pass an object containing only data.
// A function that will execute any number of functions it is passed
function noisyFarmyard() {
// Did we receive any parameters?
if (arguments.length > 0) {
// Loop around those parameters checking to see if any of them are of type ‘function’
for (var i=0; i<arguments.length; i++) {
if (typeof arguments[i] === 'function') {
arguments[i]();
}
}
}
}
noisyFarmyard(pigSays, sheepSays);

© 2013 SAP AG. All rights reserved.

Function objects can be passed as
arguments just like any other object

12
1st Class Functions: Functions as Arguments
Since JavaScript treats a function is an object, the functions created in the previous example can be
passed as parameters in the same way you would pass an object containing only data.
// A function that will execute any number of functions it is passed
function noisyFarmyard() {
// Did we receive any parameters?
if (arguments.length > 0) {
// Loop around those parameters checking to see if any of them are of type ‘function’
for (var i=0; i<arguments.length; i++) {
if (typeof arguments[i] === 'function') {
arguments[i]();
}
Because we test the data type of each
}
argument, we can determine whether the value
}
we’ve been passed is executable or not
}
noisyFarmyard(pigSays, sheepSays);

© 2013 SAP AG. All rights reserved.

13
1st Class Functions: Functions as Return Values 1/2
Having a function that returns a function is powerful tool for abstraction. Instead of a function returning
the required value, it returns a function that must be executed to obtain the required value.
// A function that works out how many animal functions have been created
function animalList() {
var listOfAnimals = [];
// Check whether a function has been created for each animal
for (var i=0; i<animalNoises.length; i=i+2) {
var fName = animalNoises[i]+"Says";

if (typeof window[fName] === 'function') {
listOfAnimals.push(fName);
}
}
return function() {
return listOfAnimals;
}

Using the array syntax for accessing an object
property, we can check whether the required
function not only exists within the Global Object,
but is also of type 'function'

}
© 2013 SAP AG. All rights reserved.

14
1st Class Functions: Functions as Return Values 1/2
Having a function that returns a function is powerful tool for abstraction. Instead of a function returning
the required value, it returns a function that must be executed to obtain the required value.
// A function that works out how many animal functions have been created
function animalList() {
var listOfAnimals = [];
// Check whether a function has been created for each animal
for (var i=0; i<animalNoises.length; i=i+2) {
var fName = animalNoises[i]+"Says";

if (typeof window[fName] === 'function') {
listOfAnimals.push(fName);
}
}
return function() {
return listOfAnimals;
}

Here, we return a function which when invoked,
will return the array called listOfAnimals

}
© 2013 SAP AG. All rights reserved.

15
1st Class Functions: Functions as Return Values 2/2
Once this function is called, it doesn't return the data we require; instead, it returns a function that
when called, will return the required data.
// A function that works out how many animal functions have been created
function animalList() {
... snip ...
}
// The call to function animalList() returns a function object. So 'animalListFunction' is
// not the data we want, but a function that when executed, will generate the data we want.
var animalListFunction = animalList();

© 2013 SAP AG. All rights reserved.

16
1st Class Functions: Functions as Return Values 2/2
Once this function is called, it doesn't return the data we require; instead, it returns a function that
when called, will return the required data.
// A function that works out how many animal functions have been created
function animalList() {
... snip ...
}
// The call to function animalList() returns a function object. So 'animalListFunction' is
// not the data we want, but a function that when executed, will generate the data we want.
var animalListFunction = animalList();

// 'animalListFunction' must now be executed using the invocation operator.
animalListFunction(); //  ["pigSays","sheepSays"]

© 2013 SAP AG. All rights reserved.

17

More Related Content

ODP
Functional programming in Javascript
Knoldus Inc.
 
PDF
Functional Javascript
guest4d57e6
 
PPT
An introduction to javascript
MD Sayem Ahmed
 
PDF
GR8Conf 2011: GPars
GR8Conf
 
PDF
PHP 8.1 - What's new and changed
Ayesh Karunaratne
 
PDF
PHP 8: What's New and Changed
Ayesh Karunaratne
 
PPTX
Kotlin on android
Kurt Renzo Acosta
 
PDF
JavaScript Robotics
Anna Gerber
 
Functional programming in Javascript
Knoldus Inc.
 
Functional Javascript
guest4d57e6
 
An introduction to javascript
MD Sayem Ahmed
 
GR8Conf 2011: GPars
GR8Conf
 
PHP 8.1 - What's new and changed
Ayesh Karunaratne
 
PHP 8: What's New and Changed
Ayesh Karunaratne
 
Kotlin on android
Kurt Renzo Acosta
 
JavaScript Robotics
Anna Gerber
 

What's hot (20)

PPTX
Advance JS and oop
Abuzer Firdousi
 
PPTX
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
PDF
Java ap is you should know
Hendrik Ebbers
 
PDF
Let's JavaScript
Paweł Dorofiejczyk
 
PDF
Save time with kotlin in android development
Adit Lal
 
PDF
Practical tips for building apps with kotlin
Adit Lal
 
PDF
Arguments Object in JavaScript
Ideas2IT Technologies
 
PPTX
Project Lambda: Evolution of Java
Can Pekdemir
 
PDF
What's in Kotlin for us - Alexandre Greschon, MyHeritage
DroidConTLV
 
PDF
Partial Functions in Scala
BoldRadius Solutions
 
PDF
Journey's End – Collection and Reduction in the Stream API
Maurice Naftalin
 
ODP
Functors, Applicatives and Monads In Scala
Knoldus Inc.
 
PDF
06. operator overloading
Haresh Jaiswal
 
PDF
Reactive Programming with JavaScript
Codemotion
 
KEY
Splat
Lee Wei Yeong
 
PDF
Cocoa heads 09112017
Vincent Pradeilles
 
PPTX
Building Mobile Apps with Android
Kurt Renzo Acosta
 
PPTX
Javascript And J Query
itsarsalan
 
PDF
The Bestiary of Pure Functional Programming
Garth Gilmour
 
PDF
Advanced functional programing in Swift
Vincent Pradeilles
 
Advance JS and oop
Abuzer Firdousi
 
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
Java ap is you should know
Hendrik Ebbers
 
Let's JavaScript
Paweł Dorofiejczyk
 
Save time with kotlin in android development
Adit Lal
 
Practical tips for building apps with kotlin
Adit Lal
 
Arguments Object in JavaScript
Ideas2IT Technologies
 
Project Lambda: Evolution of Java
Can Pekdemir
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
DroidConTLV
 
Partial Functions in Scala
BoldRadius Solutions
 
Journey's End – Collection and Reduction in the Stream API
Maurice Naftalin
 
Functors, Applicatives and Monads In Scala
Knoldus Inc.
 
06. operator overloading
Haresh Jaiswal
 
Reactive Programming with JavaScript
Codemotion
 
Cocoa heads 09112017
Vincent Pradeilles
 
Building Mobile Apps with Android
Kurt Renzo Acosta
 
Javascript And J Query
itsarsalan
 
The Bestiary of Pure Functional Programming
Garth Gilmour
 
Advanced functional programing in Swift
Vincent Pradeilles
 
Ad

Similar to JavaScript for ABAP Programmers - 5/7 Functions (20)

PPTX
Dev Concepts: Functional Programming
Svetlin Nakov
 
PPT
Basic Javascript
Bunlong Van
 
PPT
JavaScript Introductin to Functions
Charles Russell
 
PPT
25-functions.ppt
JyothiAmpally
 
ODP
Functional programming
S M Asaduzzaman
 
PPTX
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
PDF
JavaScript Functions
Colin DeCarlo
 
PPTX
Javascript for the c# developer
Salvatore Fazio
 
PPTX
Javascript basics
Solv AS
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PDF
Js in-ten-minutes
Phong Vân
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PDF
Javascript development done right
Pawel Szulc
 
PDF
Functions - complex first class citizen
Vytautas Butkus
 
PPTX
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
PDF
Scala qq
羽祈 張
 
PDF
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
PDF
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
PDF
java script functions, classes
Vijay Kalyan
 
Dev Concepts: Functional Programming
Svetlin Nakov
 
Basic Javascript
Bunlong Van
 
JavaScript Introductin to Functions
Charles Russell
 
25-functions.ppt
JyothiAmpally
 
Functional programming
S M Asaduzzaman
 
LinkedIn TBC JavaScript 100: Intro
Adam Crabtree
 
JavaScript Functions
Colin DeCarlo
 
Javascript for the c# developer
Salvatore Fazio
 
Javascript basics
Solv AS
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
Js in-ten-minutes
Phong Vân
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Javascript development done right
Pawel Szulc
 
Functions - complex first class citizen
Vytautas Butkus
 
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
Scala qq
羽祈 張
 
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
java script functions, classes
Vijay Kalyan
 
Ad

More from Chris Whealy (8)

PPTX
SAP Kapsel Plugins For Cordova
Chris Whealy
 
PPTX
Introduction to SAP Gateway and OData
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 7/7 Functional Programming
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 6/7 Inheritance
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 4/7 Scope
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 3/7 Syntax
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 2/7 Data Types
Chris Whealy
 
PDF
JavaScript for ABAP Programmers - 1/7 Introduction
Chris Whealy
 
SAP Kapsel Plugins For Cordova
Chris Whealy
 
Introduction to SAP Gateway and OData
Chris Whealy
 
JavaScript for ABAP Programmers - 7/7 Functional Programming
Chris Whealy
 
JavaScript for ABAP Programmers - 6/7 Inheritance
Chris Whealy
 
JavaScript for ABAP Programmers - 4/7 Scope
Chris Whealy
 
JavaScript for ABAP Programmers - 3/7 Syntax
Chris Whealy
 
JavaScript for ABAP Programmers - 2/7 Data Types
Chris Whealy
 
JavaScript for ABAP Programmers - 1/7 Introduction
Chris Whealy
 

Recently uploaded (20)

PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 

JavaScript for ABAP Programmers - 5/7 Functions

  • 1. JavaScript for ABAP Programmers Functions as 1st Class Citizens Chris Whealy / The RIG
  • 2. ABAP Strongly typed Syntax similar to COBOL Block Scope No equivalent concept OO using class based inheritance Imperative programming JavaScript Weakly typed Syntax derived from Java Lexical Scope Functions are 1st class citizens OO using referential inheritance Imperative or Functional programming
  • 3. 1st Class Functions Any programming language in which functions are treated as “1st class citizens” is said to implement “1st class functions”. So, what does that mean – functions have voting rights? © 2013 SAP AG. All rights reserved. 3
  • 4. 1st Class Functions Any programming language in which functions are treated as “1st class citizens” is said to implement “1st class functions”. So, what does that mean – functions have voting rights? No, a 1st class citizen is any object that can be:  Created or destroyed dynamically © 2013 SAP AG. All rights reserved. 4
  • 5. 1st Class Functions Any programming language in which functions are treated as “1st class citizens” is said to implement “1st class functions”. So, what does that mean – functions have voting rights? No, a 1st class citizen is any object that can be:  Created or destroyed dynamically  Treated as data, meaning that it can be both:  Passed as an argument to a function and  Returned as the result of running a function © 2013 SAP AG. All rights reserved. 5
  • 6. 1st Class Functions Any programming language in which functions are treated as “1st class citizens” is said to implement “1st class functions”. So, what does that mean – functions have voting rights? No, a 1st class citizen is any object that can be:  Created or destroyed dynamically  Treated as data, meaning that it can be both:  Passed as an argument to a function and  Returned as the result of running a function In JavaScript, all the above statements are applicable to functions because a function is simply an object “with an executable part”. This means a JavaScript function can be treated either as:  An object like any other data object, or as  A executable unit of code © 2013 SAP AG. All rights reserved. 6
  • 7. 1st Class Functions: Dynamic Creation A JavaScript function can be created dynamically based on the data available at runtime. // List of animals and the noises they make var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"]; // A function that returns a text string describing an animal noise function noiseMaker(name) { var n = animalNoises.indexOf(name); var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1]; return "A " + name + " says " + noise; } © 2013 SAP AG. All rights reserved. 7
  • 8. 1st Class Functions: Dynamic Creation A JavaScript function can be created dynamically based on the data available at runtime. // List of animals and the noises they make var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"]; // A function that returns a text string describing an animal noise function noiseMaker(name) { var n = animalNoises.indexOf(name); var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1]; return "A " + name + " says " + noise; } // Dynamically create some function objects specific to certain animals var pigSays = new Function("alert("" + noiseMaker("pig") + "");"); var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");"); © 2013 SAP AG. All rights reserved. 8
  • 9. 1st Class Functions: Dynamic Creation A JavaScript function can be created dynamically based on the data available at runtime. // List of animals and the noises they make var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"]; // A function that returns a text string describing an animal noise function noiseMaker(name) { var n = animalNoises.indexOf(name); var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1]; return "A " + name + " says " + noise; } // Dynamically create some function objects specific to certain animals var pigSays = new Function("alert("" + noiseMaker("pig") + "");"); var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");"); // Call these dynamically created function objects using the invocation operator pigSays(); © 2013 SAP AG. All rights reserved. 9
  • 10. 1st Class Functions: Dynamic Creation A JavaScript function can be created dynamically based on the data available at runtime. // List of animals and the noises they make var animalNoises = ["dog","woof","cat","meow","pig","oink","cow","moo"]; // A function that returns a text string describing an animal noise function noiseMaker(name) { var n = animalNoises.indexOf(name); var noise = (n == -1) ? "a sound I don't know" : animalNoises[n + 1]; return "A " + name + " says " + noise; } // Dynamically create some function objects specific to certain animals var pigSays = new Function("alert("" + noiseMaker("pig") + "");"); var sheepSays = new Function("alert("" + noiseMaker("sheep") + "");"); // Call these dynamically created function objects using the invocation operator pigSays(); sheepSays(); © 2013 SAP AG. All rights reserved. 10
  • 11. 1st Class Functions: Dynamic Destruction Setting a function object to null destroys that object. // Destroy the dynamically created function objects pigSays = null; sheepSays = null; Notice here that reference is made to the function object itself, not the result of invoking that function. In other words, the invocation operator () must not be used. © 2013 SAP AG. All rights reserved. 11
  • 12. 1st Class Functions: Functions as Arguments Since JavaScript treats a function is an object, the functions created in the previous example can be passed as parameters in the same way you would pass an object containing only data. // A function that will execute any number of functions it is passed function noisyFarmyard() { // Did we receive any parameters? if (arguments.length > 0) { // Loop around those parameters checking to see if any of them are of type ‘function’ for (var i=0; i<arguments.length; i++) { if (typeof arguments[i] === 'function') { arguments[i](); } } } } noisyFarmyard(pigSays, sheepSays); © 2013 SAP AG. All rights reserved. Function objects can be passed as arguments just like any other object 12
  • 13. 1st Class Functions: Functions as Arguments Since JavaScript treats a function is an object, the functions created in the previous example can be passed as parameters in the same way you would pass an object containing only data. // A function that will execute any number of functions it is passed function noisyFarmyard() { // Did we receive any parameters? if (arguments.length > 0) { // Loop around those parameters checking to see if any of them are of type ‘function’ for (var i=0; i<arguments.length; i++) { if (typeof arguments[i] === 'function') { arguments[i](); } Because we test the data type of each } argument, we can determine whether the value } we’ve been passed is executable or not } noisyFarmyard(pigSays, sheepSays); © 2013 SAP AG. All rights reserved. 13
  • 14. 1st Class Functions: Functions as Return Values 1/2 Having a function that returns a function is powerful tool for abstraction. Instead of a function returning the required value, it returns a function that must be executed to obtain the required value. // A function that works out how many animal functions have been created function animalList() { var listOfAnimals = []; // Check whether a function has been created for each animal for (var i=0; i<animalNoises.length; i=i+2) { var fName = animalNoises[i]+"Says"; if (typeof window[fName] === 'function') { listOfAnimals.push(fName); } } return function() { return listOfAnimals; } Using the array syntax for accessing an object property, we can check whether the required function not only exists within the Global Object, but is also of type 'function' } © 2013 SAP AG. All rights reserved. 14
  • 15. 1st Class Functions: Functions as Return Values 1/2 Having a function that returns a function is powerful tool for abstraction. Instead of a function returning the required value, it returns a function that must be executed to obtain the required value. // A function that works out how many animal functions have been created function animalList() { var listOfAnimals = []; // Check whether a function has been created for each animal for (var i=0; i<animalNoises.length; i=i+2) { var fName = animalNoises[i]+"Says"; if (typeof window[fName] === 'function') { listOfAnimals.push(fName); } } return function() { return listOfAnimals; } Here, we return a function which when invoked, will return the array called listOfAnimals } © 2013 SAP AG. All rights reserved. 15
  • 16. 1st Class Functions: Functions as Return Values 2/2 Once this function is called, it doesn't return the data we require; instead, it returns a function that when called, will return the required data. // A function that works out how many animal functions have been created function animalList() { ... snip ... } // The call to function animalList() returns a function object. So 'animalListFunction' is // not the data we want, but a function that when executed, will generate the data we want. var animalListFunction = animalList(); © 2013 SAP AG. All rights reserved. 16
  • 17. 1st Class Functions: Functions as Return Values 2/2 Once this function is called, it doesn't return the data we require; instead, it returns a function that when called, will return the required data. // A function that works out how many animal functions have been created function animalList() { ... snip ... } // The call to function animalList() returns a function object. So 'animalListFunction' is // not the data we want, but a function that when executed, will generate the data we want. var animalListFunction = animalList(); // 'animalListFunction' must now be executed using the invocation operator. animalListFunction(); //  ["pigSays","sheepSays"] © 2013 SAP AG. All rights reserved. 17