The goals
๐ช๐ปDifferent Ways of Creating Functions
โ๐ปAnonymous Functions
๐๐ปCallback Functions & Functions in Functions
๐๐ปDefault Arguments & Rest Operator
๐๐ปBind() & More
What We Already Know
Functions are "Code on Demand"
Variables and constants created in functions "belong" to that function
Functions CAN take parameters (arguments) and CAN return a value
Functions can be called multiple times (with different arguments)
Functions can be called "directly" and "indirectly"
์ด ๊ฐ์์์ "๋งค๊ฐ๋ณ์"์ "์ธ์"๋ผ๋ ๋จ์ด๋ฅผ ๊ฐ์ ์๋ฏธ๋ก ์ฌ์ฉํ๋ ๊ฒ์ ๋ฃ๊ฒ ๋ ๊ฒ๋๋ค.
์๋ฐํ ๋งํ์๋ฉด ์ด ๋์ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
๋งค๊ฐ๋ณ์๋ ํจ์๋ฅผ ์ ์ํ ๋ ๊ดํธ ์์ ์ง์ ํ๋ ๋ณ์์ ๋๋ค.
- function sayHi(name) { ... }
์ด ์์์์ name์ ๋งค๊ฐ๋ณ์์ ๋๋ค.
์ธ์๋ ํจ์๋ฅผ ํธ์ถํ ๋ ํจ์์ ์ ๋ฌํ๋ ๊ตฌ์ฒด์ ์ธ ๊ฐ์ ๋๋ค.
- sayHi('Max');
๋งค๊ฐ๋ณ์ name ์ ๋ํด 'Max' ๋ ํจ์์ ์ธ์ ์ ๋๋ค.
๋ ๊ฐ๋ ์ ๋งค์ฐ ๋ฐ์ ํ๊ฒ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก ์ ๋ ์ข ์ข "ํจ์๊ฐ ๋ฐ๋ ์ธ์๋ฅผ ์ ์ํ์" ๋ผ๋ ์์ผ๋ก ๋งํ ๊ฒ๋๋ค. ์๋ํ๋ฉด ๊ฒฐ๊ตญ ํจ์์ ์ธ์๋ฅผ ์ ์ํ๋ค๋ ๊ฒ์, ํด๋น ๋งค๊ฐ๋ณ์๋ฅผ ์ค์ ํ๋ค๋ ๋ป์ด๊ธฐ ๋๋ฌธ์ ๋๋ค (๊ทธ ๋ฐ๋์ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค).
const startGameBtn = document.getElementById('start-game-btn');
function startGame() {
console.log('Game is starting...');
}
const person = {
greet: function greet() {
console.log('Hello there!');
}
}
person.greet();
startGameBtn.addEventListener('click', startGame);
Functions are objects!
We can also store functions in objects which essentially stores the object in an object.
const startGameBtn = document.getElementById('start-game-btn');
const start = function startGame() {
console.log('Game is starting...');
}
startGameBtn.addEventListener('click', startGame);
A Different Way of Defining Functions
Anonymous Functions
Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.
An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. An anonymous function can also have multiple arguments, but only one expression.
The below enlightened syntax illustrates the declaration of anonymous function using normal declaration:
function() {
// Function Body
}
We may also declare anonymous function using arrow function technique which is shown below:
( () => {
// Function Body...
} )();
ํ์ดํ ํจ์์ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์๋ ๋ช ๊ฐ์ง ๋ค๋ฅธ ๊ตฌ๋ฌธ์ด ์์ต๋๋ค - ๋ค์์ ์ด์ ๋ํ ์์ฝ์ ๋๋ค.
์ค์ํ ์ฐธ๊ณ ์ฌํญ. ์ด ๊ธ์ ๋ ๋ถ๋ถ์ ์๋ "ํจ์๋ ๊ฐ์ฒด๋ง ๋ฐํ"ํ๋ ํน์ ์ฌ๋ก๋ฅผ ๋์น์ง ๋ง์ธ์!
1) ๊ธฐ๋ณธ ๊ตฌ๋ฌธ:
- const add = (a, b) => {
- const result = a + b;
- return result; // like in "normal" functions, parameters and return statement are OPTIONAL!
- };
์ฃผ๋ชฉํ์ค ๋งํ ์ฌํญ. ๋ง์ง๋ง์ ์ธ๋ฏธ์ฝ๋ก , ํจ์ ํค์๋ ์์, ๋งค๊ฐ๋ณ์/์ธ์์ ๊ดํธ.
2) ์ ํํ ํ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๋ ๊ฒฝ์ฐ ๋ ์งง์ ๋งค๊ฐ๋ณ์ ๊ตฌ๋ฌธ.
- const log = message => {
- console.log(message); // could also return something of course - this example just doesn't
- };
์ฃผ๋ชฉํ์ค ๋งํ ์ฌํญ. ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ ๊ดํธ๋ ์๋ตํ ์ ์์ต๋๋ค(์ ํํ ํ๋์ ์ธ์์ ๋ํด).
3) ์ธ์๋ฅผ ๋ฐ์ง ์์ ๊ฒฝ์ฐ ๋น ๋งค๊ฐ๋ณ์ ๊ดํธ.
- const greet = () => {
- console.log('Hi there!');
- };
์ฃผ๋ชฉํ์ค ๋งํ ์ฌํญ. ๊ดํธ๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค(์๋ตํ ์ ์์)
4) ์ ํํ ํ๋์ ํํ์์ด ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ ๋ ์งง์ ํจ์ ๋ณธ๋ฌธ.
- const add = (a, b) => a + b;
์ฃผ๋ชฉํ์ค ๋งํ ์ฌํญ. ์ค๊ดํธ ๋ฐ return ๋ฌธ ์๋ต ๊ฐ๋ฅ, ํํ์ ๊ฒฐ๊ณผ๋ ํญ์ ์๋ ๋ฐํ
5) ํจ์๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค(4)์ ์์์ ๊ฐ์ด ๊ตฌ๋ฌธ์ด ๋จ์ถ๋จ).
- const loadPerson = pName => ({name: pName });
์ฃผ๋ชฉํ์ค ๋งํ ์ฌํญ. ๊ฐ์ฒด์ ๋ํ ์ถ๊ฐ ๊ดํธ๊ฐ ํ์ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์ค๊ดํธ๋ ํจ์ ๋ณธ๋ฌธ์ ๊ตฌ๋ถํ๋ ๊ธฐํธ๋ก ํด์๋๋ฏ๋ก ๊ตฌ๋ฌธ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
๋ง์ง๋ง ์์๋ ํท๊ฐ๋ฆด ์ ์์ต๋๋ค. ๋ณดํต JavaScript์์ ์ค๊ดํธ๋ ํญ์ ์ ํํ ํ๋์ ์๋ฏธ๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
- const person = { name: 'Max' }; // ์ด ์ค๊ดํธ๋ ๊ฐ์ฒด ์์ฑ์ ์ฐ์
- if (something) { ... } // ์ค๊ดํธ๋ก if ๋ฌธ ๋ธ๋ก์ ํ๊ธฐํจ
๊ทธ๋ฌ๋ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ, ์ค๊ดํธ๋ ๋ ๊ฐ์ง ์๋ฏธ๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
1) ํจ์ ๋ณธ๋ฌธ ํ์(๊ธฐ๋ณธ ํ์์์)
2) ๋ฐํํ๋ ค๋ ๊ฐ์ฒด ์์ฑ(์งง์ ํจ์ ๋ณธ๋ฌธ ํ์์ผ๋ก)
JavaScript์ ์ํํ๋ ค๋ ์์ ์ "์๋ฆฌ๋ ค๋ฉด" ์์ ํ์๋ ๊ฒ์ฒ๋ผ ํํ์(์: ๊ฐ์ฒด ์์ฑ)์ ๊ดํธ๋ก ๋ฌถ์ผ์ธ์.
Rest Parameters
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
Description
A function definition's last parameter can be prefixed with ... (three U+002E FULL STOP characters), which will cause all remaining (user supplied) parameters to be placed within a standard JavaScript array. Only the last parameter in a function definition can be a rest parameter.
function myFun(a, b, ...manyMoreArgs) {
console.log("a", a);
console.log("b", b);
console.log("manyMoreArgs", manyMoreArgs);
}
myFun("one", "two", "three", "four", "five", "six");
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
Callback Function
"I will call back later!"
A callback is a function passed as an argument to another function
This technique allows a function to call another function
A callback function can run after another function has finished
function myDisplayer(some) {
document.getElementById("demo").innerHTML = some;
}
function myCalculator(num1, num2, myCallback) {
let sum = num1 + num2;
myCallback(sum);
}
myCalculator(5, 5, myDisplayer);
Bind()
Function Borrowing
With the bind() method, an object can borrow a method from another object.
The example below creates 2 objects (person and member).
The member object borrows the fullname method from the person object:
const person = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName:"Hege",
lastName: "Nilsen",
}
let fullName = person.fullName.bind(member);
Preserving this
Sometimes the bind() method has to be used to prevent loosing this.
In the following example, the person object has a display method. In the display method, this refers to the person object:
const person = {
firstName:"John",
lastName: "Doe",
display: function () {
let x = document.getElementById("demo");
x.innerHTML = this.firstName + " " + this.lastName;
}
}
person.display();
When a function is used as a callback, this is lost.
This example will try to display the person name after 3 seconds, but it will display undefined instead:
const person = {
firstName:"John",
lastName: "Doe",
display: function () {
let x = document.getElementById("demo");
x.innerHTML = this.firstName + " " + this.lastName;
}
}
setTimeout(person.display, 3000);
The bind() method solves this problem.
In the following example, the bind() method is used to bind person.display to person.
This example will display the person name after 3 seconds:
const person = {
firstName:"John",
lastName: "Doe",
display: function () {
let x = document.getElementById("demo");
x.innerHTML = this.firstName + " " + this.lastName;
}
}
let display = person.display.bind(person);
setTimeout(display, 3000);
function calculate(operation) {
const enteredNumber = getUserNumberInput();
const initialResult = currentResult;
let operator;
if (operation === 'ADD') {
currentResult += enteredNumber;
operator = '+';
} else if (operation === 'SUBTRACT') {
currentResult -= enteredNumber;
operator = '-';
} else if (operation === 'MULTIPLY') {
currentResult *= enteredNumber;
operator = '*';
} else {
currentResult /= enteredNumber;
operator = '/';
}
createAndWriteOutput(operator, initialResult, enteredNumber);
writeToLog(operation, initialResult, enteredNumber, currentResult);
}
addBtn.addEventListener('click', calculate.bind(this, 'ADD'));
subtractBtn.addEventListener('click', calculate.bind(this, 'SUBTRACT'));
multiplyBtn.addEventListener('click', calculate.bind(this, 'MULTIPLY'));
divideBtn.addEventListener('click', calculate.bind(this, 'DIVIDE'));
Sources
JavaScript Anonymous Functions - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
Rest parameters - JavaScript | MDN
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
developer.mozilla.org
https://www.w3schools.com/js/js_function_bind.asp
JavaScript Function bind() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'JavaScript - The Complete Guide 2022' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Section 8: Arrays & Iterables - Lists of Data (0) | 2022.10.18 |
---|---|
Section 7: Interacting with the HTML Page - Exploring the DOM (0) | 2022.10.17 |
Section 5: Behind The Scenes of JavaScript - How It Works, The Weird Parts & ES5 vs ES6+ (0) | 2022.10.14 |
Section 4: Control Structures - Conditional Code & Loops (0) | 2022.10.13 |
Section 3: Debugging & Efficient - Get Unstuck & Stay Productive! (0) | 2022.10.12 |