Javaの関数とメソッド



関数とは

関数は特定のタスクを実行するように設計されたコードのブロックです.
const greetings = function(){
console.log('Hello User');
};
greetings();

方法は何ですか.

メソッドは関数と全く似ていますが、呼び出されて定義された方法が異なります.
const welcome = `Welcome ${userName}`
let userName = 'Daniel';
let welcomeAlert = welcome.toUpperCase();
//* the line of code above uses a method called toUpperCase *//
console.log(welcomeAlert);

議論は何ですか.

関数パラメータは、関数定義に記載されている名前です.関数引数は、関数に渡された真の値です.
const speak = function(name = 'Daniel', time = 'night'){
//* the parameters are the variables "name" and "time" *//
console.log(`Good ${time} ${name} ); }
speak('Alejandro', 'morning')
//* the code above is the argument that alters the function's result *//

返り値
const calcArea = function(radius){
let area = 3.14 * radius**2;
return area;
}
console.log(area);
//* This function will log the results into the console *//

矢印関数

矢印関数では、短い関数構文を記述できます.関数が1つの文だけを持ち、ステートメントが値を返す場合は、括弧と戻り値のキーワードを削除できます.関数が1つの文を持つ場合にのみ動作します.
const calcArea = (radius) = {
    return 3.14 * radius**2;
}
//* This function is the same as the one above, it is more compact *//

foreachメソッドは何ですか?

このメソッドは、配列の各要素に対して1回ずつ関数を呼び出します.
let group = ['Carolina', 'Daniel', 'Piper']
const logGroup= (group, index) => {
console.log(`${index} - hello ${group}`);
group.forEach(logGroup);

コールバック関数

コールバックは他の関数への引数として渡される関数です:
function myAlarm(){
console.log('Wake UP!')};
function warning(){
console.log('You've got 10 minutes left!')};
warning();
myAlarm();

資源

www.w3schools.com
ネット忍者のUDEMYコース