Définitions Des Fonctions JavaScript - GeeksforGeeks
Définitions Des Fonctions JavaScript - GeeksforGeeks
Syntaxe:
Déclarations de fonction :
Expressions de fonction :
Constructeur de fonction :
<!DOCTYPE html>
<html>
<head>
<title>Function Declarations</title>
</head>
<p id="geeks"></p>
<script>
let var1 = GFG(40, 3);
document.getElementById(
"geeks"
).innerHTML = var1;
Sortie:
html
<!DOCTYPE html>
<html>
<head>
<title>Function Expressions</title>
</head>
<body>
<h2>GeeksForGeeks</h2>
<p id="geeks"></p>
<script>
let var1 = function (num1, num2) {
return num1 * num2;
};
document.getElementById(
"geeks"
).innerHTML = var1(20, 30);
</script>
</body>
</html>
Sortie:
html
<!DOCTYPE html>
<html>
<head>
<title>Function Expressions</title>
</head>
<body>
<h2>GeeksForGeeks</h2>
<p id="geeks"></p>
<script>
let GFG = new Function(
"num1",
"num2",
"return num1 * num2"
);
document.getElementById(
"geeks"
).innerHTML = GFG(25, 4);
</script>
</body>
</html>
Sortie:
Fonction de levage
Le hissage de fonction déplace les déclarations de fonction vers le
haut de leur portée, ce qui permet de les utiliser avant la déclaration.
Les expressions de fonction ne sont pas hissées.
html
<!DOCTYPE html>
<html>
<head>
<title>Function Hoisting</title>
</head>
<script>
GeeksForGeeks();
function GeeksForGeeks() {
document.write(
"Welcome to GeeksForGeeks"
);
}
</script>
</body>
</html>
Sortie:
html
<!DOCTYPE html>
<html>
<head>
<title>Function Hoisting</title>
</head>
<p id="geeks"></p>
<script>
(function () {
document.getElementById(
"geeks"
).innerHTML =
"GeeksForGeeks is the best way to learn";
})();
</script>
</body>
</html>
Sortie:
Lorsque la fonction est définie comme propriété d’un objet, elle est
connue sous le nom de méthode de l’objet.
Lorsque vous concevez une fonction pour créer de nouveaux objets,
elle est connue sous le nom de constructeur d’objets.
html
<!DOCTYPE html>
<html>
<head>
<title>Function Hoisting</title>
</head>
<p id="geeks"></p>
<script>
function GeeksForGeeks(num1, num2) {
return arguments.length;
}
document.getElementById(
"geeks"
).innerHTML = GeeksForGeeks(4, 3);
</script>
</body>
</html>
Sortie:
Fonctions fléchées
Les fonctions fléchées simplifient l’écriture d’expressions de fonction
en fournissant une syntaxe concise sans qu’il soit nécessaire d’utiliser
le mot-clé function, le mot-clé retour ou les accolades.
html
<!DOCTYPE html>
<html>
<head>
<title>Function Hoisting</title>
</head>
<p id="geeks"></p>
<script>
const var1 = (num1, num2) =>
num1 * num2;
document.getElementById(
"geeks"
).innerHTML = var1(5, 5);
</script>
</body>
</html>
Sortie:
R Rath… Suivre 3
Article suivant
Liaison de fonction JavaScript
Lectures similaires
Comment identifier les définitions CSS inutilisées de plusieurs…
Dans cet article, nous allons apprendre à identifier les définitions CSS
inutilisées à partir de plusieurs fichiers CSS dans un projet. Que sont le…
2 min de lecture
Comment créer une fonction qui invoque une fonction avec des…
In this article, we will learn to create a function that invokes a function
with partials appended to the arguments it receives in JavaScript.…
3 min read
How to create a function that invokes the provided function with it…
In programming, functions are used to reduce the effort of writing the
same instance of code repeatedly. In this article let us see how we can…
2 min read
Company Languages
About Us Python
Legal Java
In Media C++
Contact Us PHP
Advertise with us GoLang
GFG Corporate Solution SQL
Placement Training Program R Language
GeeksforGeeks Community Android Tutorial
Tutorials Archive