SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
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

Mais conteĂșdo relacionado

Mais procurados

Advance JS and oop
Advance JS and oopAdvance JS and oop
Advance JS and oopAbuzer Firdousi
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioLuis Atencio
 
Java ap is you should know
Java ap is you should knowJava ap is you should know
Java ap is you should knowHendrik Ebbers
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android developmentAdit Lal
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlinAdit Lal
 
Arguments Object in JavaScript
Arguments Object in JavaScriptArguments Object in JavaScript
Arguments Object in JavaScriptIdeas2IT Technologies
 
Project Lambda: Evolution of Java
Project Lambda: Evolution of JavaProject Lambda: Evolution of Java
Project Lambda: Evolution of JavaCan Pekdemir
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageDroidConTLV
 
Journey's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIJourney's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIMaurice Naftalin
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaKnoldus Inc.
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScriptCodemotion
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with AndroidKurt Renzo Acosta
 
Javascript And J Query
Javascript And J QueryJavascript And J Query
Javascript And J Queryitsarsalan
 
The Bestiary of Pure Functional Programming
The Bestiary of Pure Functional Programming The Bestiary of Pure Functional Programming
The Bestiary of Pure Functional Programming Garth Gilmour
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in SwiftVincent Pradeilles
 

Mais procurados (20)

Advance JS and oop
Advance JS and oopAdvance JS and oop
Advance JS and oop
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Java ap is you should know
Java ap is you should knowJava ap is you should know
Java ap is you should know
 
Let's JavaScript
Let's JavaScriptLet's JavaScript
Let's JavaScript
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android development
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlin
 
Arguments Object in JavaScript
Arguments Object in JavaScriptArguments Object in JavaScript
Arguments Object in JavaScript
 
Project Lambda: Evolution of Java
Project Lambda: Evolution of JavaProject Lambda: Evolution of Java
Project Lambda: Evolution of Java
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
Partial Functions in Scala
Partial Functions in ScalaPartial Functions in Scala
Partial Functions in Scala
 
Journey's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream APIJourney's End – Collection and Reduction in the Stream API
Journey's End – Collection and Reduction in the Stream API
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
 
Splat
SplatSplat
Splat
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Javascript And J Query
Javascript And J QueryJavascript And J Query
Javascript And J Query
 
The Bestiary of Pure Functional Programming
The Bestiary of Pure Functional Programming The Bestiary of Pure Functional Programming
The Bestiary of Pure Functional Programming
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in Swift
 

Semelhante a JavaScript for ABAP Programmers - 5/7 Functions

Semelhante a JavaScript for ABAP Programmers - 5/7 Functions (20)

JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
oojs
oojsoojs
oojs
 
ES6 metaprogramming unleashed
ES6 metaprogramming unleashedES6 metaprogramming unleashed
ES6 metaprogramming unleashed
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 Devs
 
JSConf: All You Can Leet
JSConf: All You Can LeetJSConf: All You Can Leet
JSConf: All You Can Leet
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
Java
JavaJava
Java
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Class
ClassClass
Class
 
Get started with YUI
Get started with YUIGet started with YUI
Get started with YUI
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 

Mais de Chris Whealy

SAP Kapsel Plugins For Cordova
SAP Kapsel Plugins For CordovaSAP Kapsel Plugins For Cordova
SAP Kapsel Plugins For CordovaChris Whealy
 
Introduction to SAP Gateway and OData
Introduction to SAP Gateway and ODataIntroduction to SAP Gateway and OData
Introduction to SAP Gateway and ODataChris Whealy
 
JavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional ProgrammingJavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional ProgrammingChris Whealy
 
JavaScript for ABAP Programmers - 6/7 Inheritance
JavaScript for ABAP Programmers - 6/7 InheritanceJavaScript for ABAP Programmers - 6/7 Inheritance
JavaScript for ABAP Programmers - 6/7 InheritanceChris Whealy
 
JavaScript for ABAP Programmers - 4/7 Scope
JavaScript for ABAP Programmers - 4/7 ScopeJavaScript for ABAP Programmers - 4/7 Scope
JavaScript for ABAP Programmers - 4/7 ScopeChris Whealy
 
JavaScript for ABAP Programmers - 3/7 Syntax
JavaScript for ABAP Programmers - 3/7 SyntaxJavaScript for ABAP Programmers - 3/7 Syntax
JavaScript for ABAP Programmers - 3/7 SyntaxChris Whealy
 
JavaScript for ABAP Programmers - 2/7 Data Types
JavaScript for ABAP Programmers - 2/7 Data TypesJavaScript for ABAP Programmers - 2/7 Data Types
JavaScript for ABAP Programmers - 2/7 Data TypesChris Whealy
 
JavaScript for ABAP Programmers - 1/7 Introduction
JavaScript for ABAP Programmers - 1/7 IntroductionJavaScript for ABAP Programmers - 1/7 Introduction
JavaScript for ABAP Programmers - 1/7 IntroductionChris Whealy
 

Mais de Chris Whealy (8)

SAP Kapsel Plugins For Cordova
SAP Kapsel Plugins For CordovaSAP Kapsel Plugins For Cordova
SAP Kapsel Plugins For Cordova
 
Introduction to SAP Gateway and OData
Introduction to SAP Gateway and ODataIntroduction to SAP Gateway and OData
Introduction to SAP Gateway and OData
 
JavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional ProgrammingJavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional Programming
 
JavaScript for ABAP Programmers - 6/7 Inheritance
JavaScript for ABAP Programmers - 6/7 InheritanceJavaScript for ABAP Programmers - 6/7 Inheritance
JavaScript for ABAP Programmers - 6/7 Inheritance
 
JavaScript for ABAP Programmers - 4/7 Scope
JavaScript for ABAP Programmers - 4/7 ScopeJavaScript for ABAP Programmers - 4/7 Scope
JavaScript for ABAP Programmers - 4/7 Scope
 
JavaScript for ABAP Programmers - 3/7 Syntax
JavaScript for ABAP Programmers - 3/7 SyntaxJavaScript for ABAP Programmers - 3/7 Syntax
JavaScript for ABAP Programmers - 3/7 Syntax
 
JavaScript for ABAP Programmers - 2/7 Data Types
JavaScript for ABAP Programmers - 2/7 Data TypesJavaScript for ABAP Programmers - 2/7 Data Types
JavaScript for ABAP Programmers - 2/7 Data Types
 
JavaScript for ABAP Programmers - 1/7 Introduction
JavaScript for ABAP Programmers - 1/7 IntroductionJavaScript for ABAP Programmers - 1/7 Introduction
JavaScript for ABAP Programmers - 1/7 Introduction
 

Último

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂșjo
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vĂĄzquez
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

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