SlideShare a Scribd company logo
JAVASCRIPT
FUNCTIONS
ADAM CRABTREE
(REPURPOSED FROM PIYUSH KATARIYA’S

HTTP://GOO.GL/ Z9WEMC)
FUNCTION
Callable first class citizen (Can be passed and return as
object)

No overloading
Definitions
• var add = new Function(‘a’, ‘b’, ‘return a + b’);
• var add = function (a, b) { return a + b; };
• function add(a, b) { return a + b;}

Blessed with
• this
• arguments
JAVASCRIPT - LENGTH
Specifies the number of arguments expected by the function.

Syntax
• functionName.length
E.g.,
• console.log( (function () {}).length );

// 0

• console.log( (function (a) {}).length );

// 1

• console.log( (function (a, b) {}).length ); // 2
INVOCATION PATTERN I
Function invocation (Direct Invocation)
• add(1, 2)
• isPalindrome(‘madam’)
this bound to global object !!!
INVOCATION PATTERN II
Method Invocation
Method => a function stored as property of object

this bound to method holder object

var obj = {
value : 0, //zero
increment : function (inc) {
this.value += typeof inc === ‘number’ ? inc : 1;

}
}
obj.increment() ; // 1
obj.increment(2); // 3
INVOCATION PATTERN III
Constructor Invocation (OO style)
var Employee = function (firstName, title) {
this.firstName = firstName;
this.title = title;
};
Employee.protoype.getName = function () { return this.name;};
Employee.protoype.getTitle = function () { return this.title;};
var employee = new Employee(‘Tom’, ‘Software Engineer’)
employee.getName(); // ‘Tom’
employee.getTitle(); // ‘Software Engineer’
INVOCATION PATTERN IV
Apply Invocation (Reflective Invocation)

var argsArray = [2, 3];
var sum = add.apply( null, argsArray);
var sum = add.call( null, 2, 3);

// 3
// 3

var firstName = Employee.getName.apply(empObject);
var firstName = Employee.getName.call(empObject);
FUNCTION AS A CLASS
var someClass = function (property) {
this.publicProperty = property;

var privateVariable = “value”;
this.publicMethod = function () {
//code for method definition

};
var privateMethod = function () {
//code for method definition
};

// return this;
}
FUNCTION AS A MODULE
var counterModule = ( function( ) {
var privateCount = 0;
function changeBy(val) {
return privateCount += val;
}
return {
increment : changeBy.bind(null, 1),
decrement : changeBy.bind(null, -1),
currentValue : function() {return privateCount;}
};
} ) ( );
JAVASCRIPT - CALL
Calls a function with a given this value and
arguments provided individually.
Syntax
• fun.call(thisArg[, arg1[, arg2[, ...]]])
JAVASCRIPT - CALL
function diplayInfo(year, month, day){
return "Name:" + this.name + ";birthday:" + year +
"." + month + "." + day;

}

var p = { name: "Jason" };
diplayInfo.call(p, 1985, 11, 5);
JAVASCRIPT - APPLY
Calls a function with a given this value
and arguments provided as an array

Syntax
• fun.apply(thisArg[, argsArray])
JAVASCRIPT - APPLY
function diplayInfo(year, month, day){
return "Name:" + this.name + ";birthday:" + year +
"." + month + "." + day;
}
var p = { name: "Jason" };
console.log(diplayInfo.apply(p, [1985, 11, 5]));
JAVASCRIPT - CALLEE
Specifies the currently executing function
callee is a property of the arguments object.
Syntax
• [function.]arguments.callee
JAVASCRIPT - CALLEE
function factorial(n){
if (n <= 0)
return 1;
else
return n * arguments.callee(n - 1);
}
factorial(4);
JAVASCRIPT - BIND
Creates a new function that, when called, has
its this keyword set to the provided value, with a given
sequence of arguments preceding any provided when the
new function is called.

Syntax
fun.bind(thisArg[, arg1[, arg2[, ...]]])
JAVASCRIPT - BIND
var x = 9;
var module = {
x: 81,

getX: function() { return this.x; }
};
module.getX(); //Answer: ?
var getX = module.getX;
getX();

//Answer: ?

var boundGetX = getX.bind(module);
boundGetX();

//Answer: ?

module.x = 100;
boundGetX(); //Answer: ?
JAVASCRIPT - BIND
var checkNumericRange = function (value) {
return value >= this.min && value <= this.max;

}

var range = { min: 10, max: 20 };

var boundCheckNumericRange =
checkNumericRange.bind(range);

var result = boundCheckNumericRange (12);
Result = ??
JAVASCRIPT - BIND

var displayArgs = function (val1, val2, val3, val4) {
console.log(val1 + " " + val2 + " " + val3 + " " + val4);
}

var emptyObject = {};

var displayArgs2 = displayArgs.bind(emptyObject, 12, "a");

displayArgs2("b", "c");

//Answer: ?
JAVASCRIPT - BIND
Function.prototype.bind = function (objToBind) {
var self = this;

return function () {
var argArr = Array.prototype.slice.call(arguments);
return self.apply(objToBind || null, argArr);
};
}

More Related Content

What's hot

C++ Functions
C++ FunctionsC++ Functions
C++ Functions
sathish sak
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
David de Boer
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Abdullah Turkistani
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Extend GraphQL with directives
Extend GraphQL with directivesExtend GraphQL with directives
Extend GraphQL with directives
Greg Bergé
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
Vincent Pradeilles
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
C++ functions
C++ functionsC++ functions
C++ functions
Mayank Jain
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
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
Luis Atencio
 
Javascript function
Javascript functionJavascript function
Javascript functionLearningTech
 
C++ functions
C++ functionsC++ functions
C++ functions
Dawood Jutt
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 

What's hot (20)

C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function C++
Function C++ Function C++
Function C++
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Extend GraphQL with directives
Extend GraphQL with directivesExtend GraphQL with directives
Extend GraphQL with directives
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Inline function
Inline functionInline function
Inline function
 
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
 
Javascript function
Javascript functionJavascript function
Javascript function
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
functions of C++
functions of C++functions of C++
functions of C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 

Viewers also liked

Writing MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptWriting MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptRoland Bouman
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
Reem Alattas
 
PostgreSQL and JSON with Python - Przemek Lewandowski
PostgreSQL and JSON  with Python - Przemek Lewandowski PostgreSQL and JSON  with Python - Przemek Lewandowski
PostgreSQL and JSON with Python - Przemek Lewandowski
Sunscrapers
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
Vijay Kalyan
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functionsbrianjihoonlee
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8James Brown
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
Esmeraldo Jr Guimbarda
 
2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)
Esmeraldo Jr Guimbarda
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
Narendran Solai Sridharan
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programming
intive
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
Istanbul Tech Talks
 
02 java programming basic
02  java programming basic02  java programming basic
02 java programming basic
Zeeshan-Shaikh
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Philip Schwarz
 
Fp java8
Fp java8Fp java8
Fp java8
Yanai Franchi
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Programming
ProgrammingProgramming
Programming
Sean Chia
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
John Ferguson Smart Limited
 

Viewers also liked (20)

Writing MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptWriting MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScript
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
PostgreSQL and JSON with Python - Przemek Lewandowski
PostgreSQL and JSON  with Python - Przemek Lewandowski PostgreSQL and JSON  with Python - Przemek Lewandowski
PostgreSQL and JSON with Python - Przemek Lewandowski
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functions
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
2java Oop
2java Oop2java Oop
2java Oop
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
 
2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programming
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
02 java programming basic
02  java programming basic02  java programming basic
02 java programming basic
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
 
Fp java8
Fp java8Fp java8
Fp java8
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Programming
ProgrammingProgramming
Programming
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
 

Similar to LinkedIn TBC JavaScript 100: Functions

25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
JyothiAmpally
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
Bret Little
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScript
Luis Atencio
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
Zsolt Mészárovics
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
Giovanni Scerra ☃
 
Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bitsChris Saylor
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
Doeun KOCH
 
Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Tsuyoshi Yamamoto
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategies
njpst8
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Building Smart Async Functions For Mobile
Building Smart Async Functions For MobileBuilding Smart Async Functions For Mobile
Building Smart Async Functions For MobileGlan Thomas
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
Bruno Scopelliti
 
Javascript Exercises
Javascript ExercisesJavascript Exercises
Javascript Exercises
SRINIVAS KOLAPARTHI
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
kvangork
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
kvangork
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgets
scottw
 

Similar to LinkedIn TBC JavaScript 100: Functions (20)

25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScript
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bits
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategies
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Building Smart Async Functions For Mobile
Building Smart Async Functions For MobileBuilding Smart Async Functions For Mobile
Building Smart Async Functions For Mobile
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Javascript Exercises
Javascript ExercisesJavascript Exercises
Javascript Exercises
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgets
 

Recently uploaded

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

LinkedIn TBC JavaScript 100: Functions

  • 1. JAVASCRIPT FUNCTIONS ADAM CRABTREE (REPURPOSED FROM PIYUSH KATARIYA’S HTTP://GOO.GL/ Z9WEMC)
  • 2. FUNCTION Callable first class citizen (Can be passed and return as object) No overloading Definitions • var add = new Function(‘a’, ‘b’, ‘return a + b’); • var add = function (a, b) { return a + b; }; • function add(a, b) { return a + b;} Blessed with • this • arguments
  • 3. JAVASCRIPT - LENGTH Specifies the number of arguments expected by the function. Syntax • functionName.length E.g., • console.log( (function () {}).length ); // 0 • console.log( (function (a) {}).length ); // 1 • console.log( (function (a, b) {}).length ); // 2
  • 4. INVOCATION PATTERN I Function invocation (Direct Invocation) • add(1, 2) • isPalindrome(‘madam’) this bound to global object !!!
  • 5. INVOCATION PATTERN II Method Invocation Method => a function stored as property of object this bound to method holder object var obj = { value : 0, //zero increment : function (inc) { this.value += typeof inc === ‘number’ ? inc : 1; } } obj.increment() ; // 1 obj.increment(2); // 3
  • 6. INVOCATION PATTERN III Constructor Invocation (OO style) var Employee = function (firstName, title) { this.firstName = firstName; this.title = title; }; Employee.protoype.getName = function () { return this.name;}; Employee.protoype.getTitle = function () { return this.title;}; var employee = new Employee(‘Tom’, ‘Software Engineer’) employee.getName(); // ‘Tom’ employee.getTitle(); // ‘Software Engineer’
  • 7. INVOCATION PATTERN IV Apply Invocation (Reflective Invocation) var argsArray = [2, 3]; var sum = add.apply( null, argsArray); var sum = add.call( null, 2, 3); // 3 // 3 var firstName = Employee.getName.apply(empObject); var firstName = Employee.getName.call(empObject);
  • 8. FUNCTION AS A CLASS var someClass = function (property) { this.publicProperty = property; var privateVariable = “value”; this.publicMethod = function () { //code for method definition }; var privateMethod = function () { //code for method definition }; // return this; }
  • 9. FUNCTION AS A MODULE var counterModule = ( function( ) { var privateCount = 0; function changeBy(val) { return privateCount += val; } return { increment : changeBy.bind(null, 1), decrement : changeBy.bind(null, -1), currentValue : function() {return privateCount;} }; } ) ( );
  • 10. JAVASCRIPT - CALL Calls a function with a given this value and arguments provided individually. Syntax • fun.call(thisArg[, arg1[, arg2[, ...]]])
  • 11. JAVASCRIPT - CALL function diplayInfo(year, month, day){ return "Name:" + this.name + ";birthday:" + year + "." + month + "." + day; } var p = { name: "Jason" }; diplayInfo.call(p, 1985, 11, 5);
  • 12. JAVASCRIPT - APPLY Calls a function with a given this value and arguments provided as an array Syntax • fun.apply(thisArg[, argsArray])
  • 13. JAVASCRIPT - APPLY function diplayInfo(year, month, day){ return "Name:" + this.name + ";birthday:" + year + "." + month + "." + day; } var p = { name: "Jason" }; console.log(diplayInfo.apply(p, [1985, 11, 5]));
  • 14. JAVASCRIPT - CALLEE Specifies the currently executing function callee is a property of the arguments object. Syntax • [function.]arguments.callee
  • 15. JAVASCRIPT - CALLEE function factorial(n){ if (n <= 0) return 1; else return n * arguments.callee(n - 1); } factorial(4);
  • 16. JAVASCRIPT - BIND Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. Syntax fun.bind(thisArg[, arg1[, arg2[, ...]]])
  • 17. JAVASCRIPT - BIND var x = 9; var module = { x: 81, getX: function() { return this.x; } }; module.getX(); //Answer: ? var getX = module.getX; getX(); //Answer: ? var boundGetX = getX.bind(module); boundGetX(); //Answer: ? module.x = 100; boundGetX(); //Answer: ?
  • 18. JAVASCRIPT - BIND var checkNumericRange = function (value) { return value >= this.min && value <= this.max; } var range = { min: 10, max: 20 }; var boundCheckNumericRange = checkNumericRange.bind(range); var result = boundCheckNumericRange (12); Result = ??
  • 19. JAVASCRIPT - BIND var displayArgs = function (val1, val2, val3, val4) { console.log(val1 + " " + val2 + " " + val3 + " " + val4); } var emptyObject = {}; var displayArgs2 = displayArgs.bind(emptyObject, 12, "a"); displayArgs2("b", "c"); //Answer: ?
  • 20. JAVASCRIPT - BIND Function.prototype.bind = function (objToBind) { var self = this; return function () { var argArr = Array.prototype.slice.call(arguments); return self.apply(objToBind || null, argArr); }; }