SlideShare a Scribd company logo
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?

More Related Content

PDF
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
PPTX
Coding convention
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
PDF
Js interpreter interpreted
PDF
The Big Three
PPT
NS2: Binding C++ and OTcl variables
PDF
04. haskell handling
PDF
Bartosz Milewski, “Re-discovering Monads in C++”
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Coding convention
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Js interpreter interpreted
The Big Three
NS2: Binding C++ and OTcl variables
04. haskell handling
Bartosz Milewski, “Re-discovering Monads in C++”

What's hot (20)

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

Viewers also liked (20)

KEY
Php Code Audits (PHP UK 2010)
PDF
Web UI performance tuning
DOC
Coding standards php
PDF
PHP Static Code Review
PDF
Coding Best practices (PHP)
PPTX
Modular & Event driven UI Architecture
PDF
PHP CODING STANDARDS
PPTX
Coding Standard And Code Review
PDF
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PDF
JavaScript and UI Architecture Best Practices
PPTX
Modern Static Code Analysis in PHP
PDF
Refactoring Legacy Code
PDF
Component Based UI Architecture - Alex Moldovan
PDF
Modern UI Architecture_ Trends and Technologies in Web Development
PPTX
UI Architecture & Web Performance
DOCX
Code review guidelines
PPT
Selenium Architecture
PDF
Content Design, UI Architecture and Content-UI-Mapping
PPTX
PHP & JavaScript & CSS Coding style
PDF
AngularJS application architecture
Php Code Audits (PHP UK 2010)
Web UI performance tuning
Coding standards php
PHP Static Code Review
Coding Best practices (PHP)
Modular & Event driven UI Architecture
PHP CODING STANDARDS
Coding Standard And Code Review
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
JavaScript and UI Architecture Best Practices
Modern Static Code Analysis in PHP
Refactoring Legacy Code
Component Based UI Architecture - Alex Moldovan
Modern UI Architecture_ Trends and Technologies in Web Development
UI Architecture & Web Performance
Code review guidelines
Selenium Architecture
Content Design, UI Architecture and Content-UI-Mapping
PHP & JavaScript & CSS Coding style
AngularJS application architecture
Ad

Similar to Functions - complex first class citizen (20)

PDF
TI1220 Lecture 6: First-class Functions
PPTX
Introduction to kotlin + spring boot demo
PDF
Fun with functions
PPT
Functions123
PPT
Functions12
PDF
10. haskell Modules
PDF
Decoupling Objects With Standard Interfaces
PPTX
functions of C++
PPT
Part 3-functions
PDF
Think Async: Asynchronous Patterns in NodeJS
PPT
25-functions.ppt
PPT
User defined functions
PPT
Introduction to Functional Programming in JavaScript
PDF
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
PDF
Composition birds-and-recursion
ODP
Machine-level Composition of Modularized Crosscutting Concerns
PDF
Functional programming ii
PPTX
Advanced JavaScript
PDF
Kotlin for android developers whats new
PDF
Javascript & Ajax Basics
TI1220 Lecture 6: First-class Functions
Introduction to kotlin + spring boot demo
Fun with functions
Functions123
Functions12
10. haskell Modules
Decoupling Objects With Standard Interfaces
functions of C++
Part 3-functions
Think Async: Asynchronous Patterns in NodeJS
25-functions.ppt
User defined functions
Introduction to Functional Programming in JavaScript
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Composition birds-and-recursion
Machine-level Composition of Modularized Crosscutting Concerns
Functional programming ii
Advanced JavaScript
Kotlin for android developers whats new
Javascript & Ajax Basics

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Electronic commerce courselecture one. Pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Electronic commerce courselecture one. Pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.

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 === { ... }