SlideShare a Scribd company logo
Javascript
function
Chapter 2
‫العارف‬
‫عبد‬
‫الباسط‬
‫أبو‬
‫شعالة‬
Table of content
1. Functions in javascript.
2. Declaration VS Expression.
3. Functions as values
4. Parameters
5. Overloading
6. Final slides with:
Here you could give a brief description of
the topic you want to talk about. For
example, if you want to talk about Mercury,
you could say that it’s the smallest planet
ABOUT THIS TOPIC
Functions are actually objects in
javascript.
The presence of an internal property
named [[Call]] is what makes function
special for any other object.
Function in javascript
The [[Call]] property is unique to functions and indicates that the
object can be executed.
[[Call]]
Declaration vs Expression
02
which begins with the function keyword and includes the name of
the function immediately following it. The contents of the function
are enclosed in braces, as shown in this declaration:
Declarations
Function declarations are hoisted
to the top of the context.
Hoisted means you can actually define a
function after it is used in code without
generating an error
Declarations
Function declarations are hoisted to the top of the context.
Hoisted means you can actually define a function after it is used in code without
generating an error, for example:
JavaScript engine hoists the function
declaration to the top and actually
executes the code as if it were
written.
Function Expression which doesn’t require a name after function
(anonymous), Instead, function expressions are typically referenced
via a variable or property, as in this expression:
Expression
This code actually assigns a
function value to the
variable sub.
semicolon at the end.
Functions as values
03
you can use Javascript Functions just as you do any other objects.
You can assign them to variables, add them to objects, pass them
to other functions as arguments, and return them from functions.
Functions as values
Both greet and greet are now
pointing to the same function,
and that means either can be
executed
take a look at the same code rewritten to use the Function
constructor:
Functions as values
Parameters
04
You can pass any number of parameters to any function
without causing an error.
Parameters
function parameters are actually stored as an array-like structure called
arguments.
there is a length property to determine how many values are present
Here’s a simple example using arguments and
function arity; note that the number of
arguments passed to the function has no effect
on the reported arity:
Paramters Single named parameter
The length property is 1 because there is a
single named parameter
No error here when the second
parameter is passed
using arguments is actually more
effective than naming parameters
suppose you want to create a
function that accepts any number
of parameters and returns their
sum
Overloading
05
Overloading is the ability of a single function
to have multiple signatures
Overlaoding
A function signature is made up of the function name plus the number
and type of parameters the function expects.
JavaScript functions don’t actually have signatures. A lack of function
signatures also means a lack of function overloading. Look at what
happens when you try to declare two functions with the same name:
Lack of function signature
In JavaScript, when you define
multiple functions with the
same name, the one that
appears last in your code wins.
Looking at the code this way makes it clear why the previous code didn’t work. A
function object is being assigned to sayMessage twice in a row, so it makes sense
that the first function object would be lost
Mimic the function overloading
Object methods
06
The this Object
07
When a property value is actually a function, the property is considered
a method.
Object method
For example, in the following code,
the person variable is assigned an
object literal with a name property
and a method called sayName.
You can then call the method directly
from the object as in
person.sayName("Alaref").
You may have noticed something strange in the previous example. The sayName()
method references person.name directly which creates tight coupling between the
method and the object.
The this object
There are two Problems of tight coupling:
1 - if you change the variable name, you also need to remember to
change the reference to that name in the method.
2 - this sort of tight coupling makes it difficult to use the same
function for different objects.
Every scope in JavaScript has a this object that represents the calling object for
the function.
In the global scope, this represents the global object (window in web browsers).
The this object
When a function is called while attached to an object, the value of this is equal
to that object by default. So, instead of directly referencing an object inside a
method, you can reference this instead
The this object
For example, you can rewrite the code from the previous example to use this:
This code works the same as the
earlier version, but this time,
sayName() references this instead of
person.
you can easily change the name of the variable or
even reuse the function on different objects.
The this object
In this example, a function called sayName is
defined first. Then, two object literals are
created that assign sayName to be equal to the
sayNameForAll function
The last part of this example defines a global
variable called name. When sayNameForAll() is
called directly
Property of
Global object
Reference
values
Changing this
08
Even though this is typically assigned automatically, you can change its value
to achieve different goals. There are three functions methods that allow you
to change the value of this :
Changing this
● The call() method.
● The apply() method.
● The bind() method.
The call() method
09
● The call() method executes the function with a particular this value and
with specific parameters.
● The first parameter of call() is the value to which this should be equal
when the function is executed
● All subsequent parameters are the parameters that should be passed into
the function
The call() method
For example, suppose you update sayNameForAll() to take a parameter
The call() method
no parentheses after the function name
because it is accessed as an object
The apply() method works exactly the same as call() except that it accepts only
two parameters:
1. the value for this.
2. and an array or array-like object of parameters to pass to the function
instead of individually naming each parameter using call(), you can easily
pass arrays to apply() as the second argument.
Note that you can use an arguments object as the second parameter
The apply() method
This example shows the apply() method in action:
The apply() method
The first argument to bind() is the this value for the new function. All
other arguments represent named parameters that should be
permanently set in the new function.
The bind() method
The following code shows two examples that use bind(). You create the sayNameForPerson1()
function by binding the this value to person1, while sayNameForPerson2() binds this to person2
and binds the first parameter as "person2".
The bind() method

More Related Content

What's hot

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentationAdhoura Academy
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and FunctionsJussi Pohjolainen
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Java script
Java scriptJava script
Java script
Sadeek Mohammed
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
Rajat Saxena
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Andres Baravalle
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
Dominic Arrojado
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
Nicholas Jansma
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
Jalpesh Vasa
 

What's hot (20)

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Js ppt
Js pptJs ppt
Js ppt
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Java script
Java scriptJava script
Java script
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 

Similar to Javascript functions

Java script function
Java script functionJava script function
Java script function
suresh raj sharma
 
Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in php
Ashish Chamoli
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
venud11
 
Function
FunctionFunction
Function
MuhammadBakri13
 
Objects and classes in Visual Basic
Objects and classes in Visual BasicObjects and classes in Visual Basic
Objects and classes in Visual Basic
Sangeetha Sg
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
NUST Stuff
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
tarunsharmaug23
 
4. Types, Objects and NameSPACES in. Net.pptx
4. Types, Objects and NameSPACES in. Net.pptx4. Types, Objects and NameSPACES in. Net.pptx
4. Types, Objects and NameSPACES in. Net.pptx
ns1978314
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
Mars Devs
 
Scala tutorial
Scala tutorialScala tutorial
Scala tutorial
wafianedjma
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
Actionscript
ActionscriptActionscript
Actionscript
saad_darwish
 
Function in C++
Function in C++Function in C++
Function in C++
Prof Ansari
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
Igor Crvenov
 

Similar to Javascript functions (20)

Java script function
Java script functionJava script function
Java script function
 
Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in php
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Function
FunctionFunction
Function
 
Objects and classes in Visual Basic
Objects and classes in Visual BasicObjects and classes in Visual Basic
Objects and classes in Visual Basic
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
 
4. Types, Objects and NameSPACES in. Net.pptx
4. Types, Objects and NameSPACES in. Net.pptx4. Types, Objects and NameSPACES in. Net.pptx
4. Types, Objects and NameSPACES in. Net.pptx
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
Scala tutorial
Scala tutorialScala tutorial
Scala tutorial
 
Scala tutorial
Scala tutorialScala tutorial
Scala tutorial
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Actionscript
ActionscriptActionscript
Actionscript
 
Function in C++
Function in C++Function in C++
Function in C++
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 

More from Alaref Abushaala

الطريق لكي تصبح مطور برمجيات.pptx
الطريق لكي تصبح مطور برمجيات.pptxالطريق لكي تصبح مطور برمجيات.pptx
الطريق لكي تصبح مطور برمجيات.pptx
Alaref Abushaala
 
Javascript Object Patterns.pptx
Javascript Object Patterns.pptxJavascript Object Patterns.pptx
Javascript Object Patterns.pptx
Alaref Abushaala
 
Distributed Database
Distributed Database Distributed Database
Distributed Database
Alaref Abushaala
 
التخطيط لإختبار البرمجيات.pptx
التخطيط لإختبار البرمجيات.pptxالتخطيط لإختبار البرمجيات.pptx
التخطيط لإختبار البرمجيات.pptx
Alaref Abushaala
 
Specialized parallel computing
Specialized parallel computingSpecialized parallel computing
Specialized parallel computing
Alaref Abushaala
 
Travel guide app_prototyping_presentation
Travel guide app_prototyping_presentationTravel guide app_prototyping_presentation
Travel guide app_prototyping_presentation
Alaref Abushaala
 
وحدة المعالجة المركزية
وحدة المعالجة المركزيةوحدة المعالجة المركزية
وحدة المعالجة المركزية
Alaref Abushaala
 
إدمان العصر الحديث
إدمان العصر الحديثإدمان العصر الحديث
إدمان العصر الحديث
Alaref Abushaala
 
دور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالمدور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالم
Alaref Abushaala
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
Alaref Abushaala
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
Alaref Abushaala
 

More from Alaref Abushaala (11)

الطريق لكي تصبح مطور برمجيات.pptx
الطريق لكي تصبح مطور برمجيات.pptxالطريق لكي تصبح مطور برمجيات.pptx
الطريق لكي تصبح مطور برمجيات.pptx
 
Javascript Object Patterns.pptx
Javascript Object Patterns.pptxJavascript Object Patterns.pptx
Javascript Object Patterns.pptx
 
Distributed Database
Distributed Database Distributed Database
Distributed Database
 
التخطيط لإختبار البرمجيات.pptx
التخطيط لإختبار البرمجيات.pptxالتخطيط لإختبار البرمجيات.pptx
التخطيط لإختبار البرمجيات.pptx
 
Specialized parallel computing
Specialized parallel computingSpecialized parallel computing
Specialized parallel computing
 
Travel guide app_prototyping_presentation
Travel guide app_prototyping_presentationTravel guide app_prototyping_presentation
Travel guide app_prototyping_presentation
 
وحدة المعالجة المركزية
وحدة المعالجة المركزيةوحدة المعالجة المركزية
وحدة المعالجة المركزية
 
إدمان العصر الحديث
إدمان العصر الحديثإدمان العصر الحديث
إدمان العصر الحديث
 
دور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالمدور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالم
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
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
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
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
 
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
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
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
 
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
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
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
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
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...
 
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
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
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
 
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...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

Javascript functions

  • 2. Table of content 1. Functions in javascript. 2. Declaration VS Expression. 3. Functions as values 4. Parameters 5. Overloading 6. Final slides with:
  • 3. Here you could give a brief description of the topic you want to talk about. For example, if you want to talk about Mercury, you could say that it’s the smallest planet ABOUT THIS TOPIC
  • 4. Functions are actually objects in javascript. The presence of an internal property named [[Call]] is what makes function special for any other object. Function in javascript
  • 5. The [[Call]] property is unique to functions and indicates that the object can be executed. [[Call]]
  • 7. which begins with the function keyword and includes the name of the function immediately following it. The contents of the function are enclosed in braces, as shown in this declaration: Declarations Function declarations are hoisted to the top of the context. Hoisted means you can actually define a function after it is used in code without generating an error
  • 8. Declarations Function declarations are hoisted to the top of the context. Hoisted means you can actually define a function after it is used in code without generating an error, for example: JavaScript engine hoists the function declaration to the top and actually executes the code as if it were written.
  • 9. Function Expression which doesn’t require a name after function (anonymous), Instead, function expressions are typically referenced via a variable or property, as in this expression: Expression This code actually assigns a function value to the variable sub. semicolon at the end.
  • 11. you can use Javascript Functions just as you do any other objects. You can assign them to variables, add them to objects, pass them to other functions as arguments, and return them from functions. Functions as values Both greet and greet are now pointing to the same function, and that means either can be executed
  • 12. take a look at the same code rewritten to use the Function constructor: Functions as values
  • 14. You can pass any number of parameters to any function without causing an error. Parameters function parameters are actually stored as an array-like structure called arguments. there is a length property to determine how many values are present
  • 15. Here’s a simple example using arguments and function arity; note that the number of arguments passed to the function has no effect on the reported arity: Paramters Single named parameter The length property is 1 because there is a single named parameter No error here when the second parameter is passed
  • 16. using arguments is actually more effective than naming parameters suppose you want to create a function that accepts any number of parameters and returns their sum
  • 18. Overloading is the ability of a single function to have multiple signatures Overlaoding A function signature is made up of the function name plus the number and type of parameters the function expects.
  • 19. JavaScript functions don’t actually have signatures. A lack of function signatures also means a lack of function overloading. Look at what happens when you try to declare two functions with the same name: Lack of function signature In JavaScript, when you define multiple functions with the same name, the one that appears last in your code wins.
  • 20. Looking at the code this way makes it clear why the previous code didn’t work. A function object is being assigned to sayMessage twice in a row, so it makes sense that the first function object would be lost
  • 21. Mimic the function overloading
  • 24. When a property value is actually a function, the property is considered a method. Object method For example, in the following code, the person variable is assigned an object literal with a name property and a method called sayName. You can then call the method directly from the object as in person.sayName("Alaref").
  • 25. You may have noticed something strange in the previous example. The sayName() method references person.name directly which creates tight coupling between the method and the object. The this object There are two Problems of tight coupling: 1 - if you change the variable name, you also need to remember to change the reference to that name in the method. 2 - this sort of tight coupling makes it difficult to use the same function for different objects.
  • 26. Every scope in JavaScript has a this object that represents the calling object for the function. In the global scope, this represents the global object (window in web browsers). The this object When a function is called while attached to an object, the value of this is equal to that object by default. So, instead of directly referencing an object inside a method, you can reference this instead
  • 27. The this object For example, you can rewrite the code from the previous example to use this: This code works the same as the earlier version, but this time, sayName() references this instead of person.
  • 28. you can easily change the name of the variable or even reuse the function on different objects. The this object In this example, a function called sayName is defined first. Then, two object literals are created that assign sayName to be equal to the sayNameForAll function The last part of this example defines a global variable called name. When sayNameForAll() is called directly Property of Global object Reference values
  • 30. Even though this is typically assigned automatically, you can change its value to achieve different goals. There are three functions methods that allow you to change the value of this : Changing this ● The call() method. ● The apply() method. ● The bind() method.
  • 32. ● The call() method executes the function with a particular this value and with specific parameters. ● The first parameter of call() is the value to which this should be equal when the function is executed ● All subsequent parameters are the parameters that should be passed into the function The call() method
  • 33. For example, suppose you update sayNameForAll() to take a parameter The call() method no parentheses after the function name because it is accessed as an object
  • 34. The apply() method works exactly the same as call() except that it accepts only two parameters: 1. the value for this. 2. and an array or array-like object of parameters to pass to the function instead of individually naming each parameter using call(), you can easily pass arrays to apply() as the second argument. Note that you can use an arguments object as the second parameter The apply() method
  • 35. This example shows the apply() method in action: The apply() method
  • 36. The first argument to bind() is the this value for the new function. All other arguments represent named parameters that should be permanently set in the new function. The bind() method
  • 37. The following code shows two examples that use bind(). You create the sayNameForPerson1() function by binding the this value to person1, while sayNameForPerson2() binds this to person2 and binds the first parameter as "person2". The bind() method