SlideShare uma empresa Scribd logo
1 de 37
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

Mais conteúdo relacionado

Mais procurados

JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 

Mais procurados (20)

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
Java script
Java scriptJava script
Java script
 
Jquery
JqueryJquery
Jquery
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
The New JavaScript: ES6
The New JavaScript: ES6The New JavaScript: ES6
The New JavaScript: ES6
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Js ppt
Js pptJs ppt
Js ppt
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 

Semelhante a Javascript functions

Semelhante a 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 .
 
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
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 

Mais de Alaref Abushaala

Mais de 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
 
وحدة المعالجة المركزية
وحدة المعالجة المركزيةوحدة المعالجة المركزية
وحدة المعالجة المركزية
 
إدمان العصر الحديث
إدمان العصر الحديثإدمان العصر الحديث
إدمان العصر الحديث
 
دور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالمدور الاتصالات والتحكم الالي في تطور العالم
دور الاتصالات والتحكم الالي في تطور العالم
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
 
منظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظممنظومة إدارة السجناء | تحليل وتصميم نظم
منظومة إدارة السجناء | تحليل وتصميم نظم
 

Último

Último (20)

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
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...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

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