SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
Functions - complex first
class citizen
Vytautas Butkus
Content
● What is a function?
● Defining functions
● Calling functions
● Context
What is a function?..
Mathematical:
f(x) -> x * x
Javascript:
function (x) {
return x * x;
}
Square Function
...what else is a function?
● Functions are objects
○ function() {} instanceof Object //true
○ Can be passed as an argument
- doMagic(function () {})
- function magic() {}; addPower(magic);
○ Can have other objects/functions as properties
- typeof $ //"function"
- $.Event, $.ajax, $.each, etc..
○ Always passed by reference
- function addPower (fn) { fn.fire = true; }
- magic.fire //true;
...what else is a function?
● Function is a scope provider
○ No scope in if (condition) { ... } else { ... } blocks
○ Variables are scoped to function's body block
...what else is a function?
● Function is a scope provider
○ No scope in if (condition) { ... } else { ... } blocks
○ Variables are scoped to function's body block
Content
● What is a function?
● Defining functions
● Calling functions
● Context
Defining functions
● Function expression
○ var expression = function (arg1, arg2) { ... }
○ Anonymous callback functions
● Function declaration
○ function declaredFunction (arg1, arg2) { ... }
○ always hoisted to the top
○ named functions
● IIFE (Immediately-Invoked Function Expression)
○ (function () { ... })(); //Anonymous function
○ Used to create variable scopes (basically memory leaks)
○ Used to define modules (must return reference)
Named functions
(function firstClosure () {
(function secondClosure () {
(function thirdClosure () {
(function fourthClosure () {
console.log(1);
debugger
})()
})()
})()
})()
(function () {
(function () {
(function () {
(function () {
console.log(1);
debugger
})()
})()
})()
})()
VS
var activity = function workout () { ... }
totally legal
Content
● What is a function?
● Defining functions
● Calling functions
● Context
Calling functions
● Function(arg1, arg2,...);
● Function.call(context, arg1, arg2,...);
● Function.apply(context, [arguments]);
● setTimeout(Function, delay, arg1, arg2,...);
● setInterval(Function, delay, arg1, arg2,...);
○ requestAnimationFrame(...);
● new Function(arg1, arg2,...);
Calling functions
● Function(arg1, arg2,...);
● Function.call(context, arg1, arg2,...);
● Function.apply(context, [arguments]);
● setTimeout(Function, delay, arg1, arg2,...);
● setInterval(Function, delay, arg1, arg2,...);
● new Function(arg1, arg2,...);
Black magic explained:
1. New object { } is created
2. Assign object { } internal [[Prototype]] property to the prototype
property of Person
- object { }.[[prototype]] = Person.prototype;
3. Call Person as normal, passing it the object { } as this:
- Person.call(object { }, 'Jim');
Content
● What is a function?
● Defining functions
● Calling functions
● Context
Context
● Context === this
● this === { ... }
Context
● Context === this
● this === { ... }
Thank you! Questions?

Mais conteúdo relacionado

Mais procurados

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2
Bahul Neel Upadhyaya
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 

Mais procurados (20)

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Garbage collector in python
Garbage collector in pythonGarbage collector in python
Garbage collector in python
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Linguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and ThreadsLinguistic Symbiosis between Actors and Threads
Linguistic Symbiosis between Actors and Threads
 
Python GC
Python GCPython GC
Python GC
 
20170714 concurrency in julia
20170714 concurrency in julia20170714 concurrency in julia
20170714 concurrency in julia
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Queue
QueueQueue
Queue
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
20170317 functional programming in julia
20170317 functional programming in julia20170317 functional programming in julia
20170317 functional programming in julia
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Constructors
ConstructorsConstructors
Constructors
 
The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 

Destaque

Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
Damien Seguy
 
Web UI performance tuning
Web UI performance tuningWeb UI performance tuning
Web UI performance tuning
Andy Pemberton
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web Development
Suresh Patidar
 

Destaque (20)

Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
 
Web UI performance tuning
Web UI performance tuningWeb UI performance tuning
Web UI performance tuning
 
Coding standards php
Coding standards phpCoding standards php
Coding standards php
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code Review
 
Coding Best practices (PHP)
Coding Best practices (PHP)Coding Best practices (PHP)
Coding Best practices (PHP)
 
Modular & Event driven UI Architecture
Modular & Event driven UI ArchitectureModular & Event driven UI Architecture
Modular & Event driven UI Architecture
 
PHP CODING STANDARDS
PHP CODING STANDARDSPHP CODING STANDARDS
PHP CODING STANDARDS
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
 
Modern Static Code Analysis in PHP
Modern Static Code Analysis in PHPModern Static Code Analysis in PHP
Modern Static Code Analysis in PHP
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Component Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex MoldovanComponent Based UI Architecture - Alex Moldovan
Component Based UI Architecture - Alex Moldovan
 
Modern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web DevelopmentModern UI Architecture_ Trends and Technologies in Web Development
Modern UI Architecture_ Trends and Technologies in Web Development
 
UI Architecture & Web Performance
UI Architecture & Web PerformanceUI Architecture & Web Performance
UI Architecture & Web Performance
 
Code review guidelines
Code review guidelinesCode review guidelines
Code review guidelines
 
Selenium Architecture
Selenium ArchitectureSelenium Architecture
Selenium Architecture
 
Content Design, UI Architecture and Content-UI-Mapping
Content Design, UI Architecture and Content-UI-MappingContent Design, UI Architecture and Content-UI-Mapping
Content Design, UI Architecture and Content-UI-Mapping
 
PHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding stylePHP & JavaScript & CSS Coding style
PHP & JavaScript & CSS Coding style
 
AngularJS application architecture
AngularJS application architectureAngularJS application architecture
AngularJS application architecture
 

Semelhante a Functions - complex first class citizen

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Thomas Weinert
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
ankita44
 
User defined functions
User defined functionsUser defined functions
User defined functions
shubham_jangid
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul
 

Semelhante a Functions - complex first class citizen (20)

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Fun with functions
Fun with functionsFun with functions
Fun with functions
 
Functions12
Functions12Functions12
Functions12
 
Functions123
Functions123 Functions123
Functions123
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
 
functions of C++
functions of C++functions of C++
functions of C++
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
 
Composition birds-and-recursion
Composition birds-and-recursionComposition birds-and-recursion
Composition birds-and-recursion
 
Machine-level Composition of Modularized Crosscutting Concerns
Machine-level Composition of Modularized Crosscutting ConcernsMachine-level Composition of Modularized Crosscutting Concerns
Machine-level Composition of Modularized Crosscutting Concerns
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Kotlin for android developers whats new
Kotlin for android developers whats newKotlin for android developers whats new
Kotlin for android developers whats new
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Functions - complex first class citizen

  • 1. Functions - complex first class citizen Vytautas Butkus
  • 2. Content ● What is a function? ● Defining functions ● Calling functions ● Context
  • 3. What is a function?.. Mathematical: f(x) -> x * x Javascript: function (x) { return x * x; } Square Function
  • 4. ...what else is a function? ● Functions are objects ○ function() {} instanceof Object //true ○ Can be passed as an argument - doMagic(function () {}) - function magic() {}; addPower(magic); ○ Can have other objects/functions as properties - typeof $ //"function" - $.Event, $.ajax, $.each, etc.. ○ Always passed by reference - function addPower (fn) { fn.fire = true; } - magic.fire //true;
  • 5. ...what else is a function? ● Function is a scope provider ○ No scope in if (condition) { ... } else { ... } blocks ○ Variables are scoped to function's body block
  • 6. ...what else is a function? ● Function is a scope provider ○ No scope in if (condition) { ... } else { ... } blocks ○ Variables are scoped to function's body block
  • 7. Content ● What is a function? ● Defining functions ● Calling functions ● Context
  • 8. Defining functions ● Function expression ○ var expression = function (arg1, arg2) { ... } ○ Anonymous callback functions ● Function declaration ○ function declaredFunction (arg1, arg2) { ... } ○ always hoisted to the top ○ named functions ● IIFE (Immediately-Invoked Function Expression) ○ (function () { ... })(); //Anonymous function ○ Used to create variable scopes (basically memory leaks) ○ Used to define modules (must return reference)
  • 9. Named functions (function firstClosure () { (function secondClosure () { (function thirdClosure () { (function fourthClosure () { console.log(1); debugger })() })() })() })() (function () { (function () { (function () { (function () { console.log(1); debugger })() })() })() })() VS var activity = function workout () { ... } totally legal
  • 10. Content ● What is a function? ● Defining functions ● Calling functions ● Context
  • 11. Calling functions ● Function(arg1, arg2,...); ● Function.call(context, arg1, arg2,...); ● Function.apply(context, [arguments]); ● setTimeout(Function, delay, arg1, arg2,...); ● setInterval(Function, delay, arg1, arg2,...); ○ requestAnimationFrame(...); ● new Function(arg1, arg2,...);
  • 12. Calling functions ● Function(arg1, arg2,...); ● Function.call(context, arg1, arg2,...); ● Function.apply(context, [arguments]); ● setTimeout(Function, delay, arg1, arg2,...); ● setInterval(Function, delay, arg1, arg2,...); ● new Function(arg1, arg2,...); Black magic explained: 1. New object { } is created 2. Assign object { } internal [[Prototype]] property to the prototype property of Person - object { }.[[prototype]] = Person.prototype; 3. Call Person as normal, passing it the object { } as this: - Person.call(object { }, 'Jim');
  • 13. Content ● What is a function? ● Defining functions ● Calling functions ● Context
  • 14. Context ● Context === this ● this === { ... }
  • 15. Context ● Context === this ● this === { ... }