SlideShare uma empresa Scribd logo
1 de 20
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);
};
}

Mais conteúdo relacionado

Mais procurados

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
 
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 directivesGreg Bergé
 
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 AtencioLuis Atencio
 
Javascript function
Javascript functionJavascript function
Javascript functionLearningTech
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 

Mais procurados (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
 

Destaque

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, classesVijay 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 LambdasGanesh 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
 
Java Script - Object-Oriented Programming
Java Script - Object-Oriented ProgrammingJava Script - Object-Oriented Programming
Java Script - Object-Oriented Programmingintive
 
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 ProgrammingIstanbul Tech Talks
 
02 java programming basic
02  java programming basic02  java programming basic
02 java programming basicZeeshan-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
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 

Destaque (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
 

Semelhante a LinkedIn TBC JavaScript 100: Functions

JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
Thinking Functionally with JavaScript
Thinking Functionally with JavaScriptThinking Functionally with JavaScript
Thinking Functionally with JavaScriptLuis Atencio
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bitsChris Saylor
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun 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 Strategiesnjpst8
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
Building Smart Async Functions For Mobile
Building Smart Async Functions For MobileBuilding Smart Async Functions For Mobile
Building Smart Async Functions For MobileGlan Thomas
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScriptkvangork
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascriptkvangork
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgetsscottw
 

Semelhante a 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
 

Último

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
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...Martijn de Jong
 
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
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
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 2024The Digital Insurer
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 

Último (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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...
 
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
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

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