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

PPTX
Java script
PPT
JavaScript Functions
PDF
JavaScript Functions
PDF
Functional Javascript
PPTX
Javascript Function
PDF
Currying and Partial Function Application (PFA)
PDF
DRYing to Monad in Java8
PPTX
C++ programming function
Java script
JavaScript Functions
JavaScript Functions
Functional Javascript
Javascript Function
Currying and Partial Function Application (PFA)
DRYing to Monad in Java8
C++ programming function

What's hot (20)

PPT
C++ Functions
PDF
Being functional in PHP (DPC 2016)
PPT
Functions in c++
PPTX
Function C++
PPT
16717 functions in C++
 
PDF
Extend GraphQL with directives
PDF
Cocoa heads 09112017
PDF
03 function overloading
PPT
C++ functions
PPT
Function overloading(C++)
PPTX
Inline function
PPTX
Functional Programming in JavaScript by Luis Atencio
PPTX
Javascript function
PPT
C++ functions
PPT
Lecture#6 functions in c++
PDF
Monads in Swift
PPTX
Inline Functions and Default arguments
PPTX
functions of C++
PPT
Functions in C++
PDF
Java Script Best Practices
C++ Functions
Being functional in PHP (DPC 2016)
Functions in c++
Function C++
16717 functions in C++
 
Extend GraphQL with directives
Cocoa heads 09112017
03 function overloading
C++ functions
Function overloading(C++)
Inline function
Functional Programming in JavaScript by Luis Atencio
Javascript function
C++ functions
Lecture#6 functions in c++
Monads in Swift
Inline Functions and Default arguments
functions of C++
Functions in C++
Java Script Best Practices
Ad

Viewers also liked (20)

PDF
Writing MySQL User-defined Functions in JavaScript
PPT
JavaScript Functions
PPTX
PostgreSQL and JSON with Python - Przemek Lewandowski
PDF
java script functions, classes
PPTX
Week 5 java script functions
PPTX
Lambda functions in java 8
PDF
Functional Programming in Java 8 - Exploiting Lambdas
PDF
2java Oop
PDF
TM 2nd qtr-3ndmeeting(java script-functions)
PDF
2ndQuarter2ndMeeting(formatting number)
PPTX
Functional Programming in Java
PDF
Java Script - Object-Oriented Programming
PPTX
02 java programming basic
PDF
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
PDF
Fp java8
PDF
Functional programming with Java 8
PPT
Programming
PDF
Functional programming in java
PDF
Basic java for Android Developer
Writing MySQL User-defined Functions in JavaScript
JavaScript Functions
PostgreSQL and JSON with Python - Przemek Lewandowski
java script functions, classes
Week 5 java script functions
Lambda functions in java 8
Functional Programming in Java 8 - Exploiting Lambdas
2java Oop
TM 2nd qtr-3ndmeeting(java script-functions)
2ndQuarter2ndMeeting(formatting number)
Functional Programming in Java
Java Script - Object-Oriented Programming
02 java programming basic
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Fp java8
Functional programming with Java 8
Programming
Functional programming in java
Basic java for Android Developer
Ad

Similar to LinkedIn TBC JavaScript 100: Functions (20)

PPTX
JavaScript (without DOM)
PPT
25-functions.ppt
PPTX
11. session 11 functions and objects
PPT
JavaScript - Programming Languages course
PPTX
Javascript functions
PDF
Rediscovering JavaScript: The Language Behind The Libraries
PPTX
Lecture 4- Javascript Function presentation
PDF
The many facets of code reuse in JavaScript
PDF
The mighty js_function
PDF
JavaScript Primer
PDF
ES6 metaprogramming unleashed
PDF
PPTX
Awesomeness of JavaScript…almost
PPTX
Javascript: master this
PDF
Js in-ten-minutes
PPTX
Javascript Objects Deep Dive
PPTX
Javascript
PPTX
JavaScript- Functions and arrays.pptx
PDF
JavaScript: Patterns, Part 3
PPTX
Intro to Javascript
JavaScript (without DOM)
25-functions.ppt
11. session 11 functions and objects
JavaScript - Programming Languages course
Javascript functions
Rediscovering JavaScript: The Language Behind The Libraries
Lecture 4- Javascript Function presentation
The many facets of code reuse in JavaScript
The mighty js_function
JavaScript Primer
ES6 metaprogramming unleashed
Awesomeness of JavaScript…almost
Javascript: master this
Js in-ten-minutes
Javascript Objects Deep Dive
Javascript
JavaScript- Functions and arrays.pptx
JavaScript: Patterns, Part 3
Intro to Javascript

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Modernizing your data center with Dell and AMD
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
NewMind AI Monthly Chronicles - July 2025
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Modernizing your data center with Dell and AMD
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

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); }; }