[ Web Proxy ]
URL:
Viewing: https://zh.javascript.info/function-expressions [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

JavaScript

function sayHi() {
  alert( "Hello" );
}

let sayHi = function() {
  alert( "Hello" );
};

sayHi function() { alert("Hello"); }

=

function

sayHi

sayHi

alert

function sayHi() {
  alert( "Hello" );
}

alert( sayHi ); // 

sayHi JavaScript

JavaScript

sayHi()

function sayHi() {   // (1) 
  alert( "Hello" );
}

let func = sayHi;    // (2) 

func(); // Hello     // (3) 
sayHi(); // Hello    //     

  1. (1) sayHi
  2. (2) sayHi funcsayHi func = sayHi() sayHi() func sayHi
  3. sayHi() func()

sayHi

let sayHi = function() { // (1) 
  alert( "Hello" );
};

let func = sayHi;
// ...

;

function sayHi() {
  // ...
}

let sayHi = function() {
  // ...
};

let sayHi = ...; function() {} ;

let sayHi = 5;

ask(question, yes, no)

question
yes
Yes
no
No

question yes() no()

function ask(question, yes, no) {
  if (confirm(question)) yes()
  else no();
}

function showOk() {
  alert( "You agreed." );
}

function showCancel() {
  alert( "You canceled the execution." );
}

//  showOk  showCancel  ask
ask("Do you agree?", showOk, showCancel);

confirm

ask showOk showCancel

showOk yes showCancel no

function ask(question, yes, no) {
  if (confirm(question)) yes()
  else no();
}

ask(
  "Do you agree?",
  function() { alert("You agreed."); },
  function() { alert("You canceled the execution."); }
);

ask(...) ask

JavaScript

action

vs

  • // 
    function sum(a, b) {
      return a + b;
    }
  • =

    // 
    let sum = function(a, b) {
      return a + b;
    };

JavaScript

let sum = function

JavaScript

sayHi("John"); // Hello, John

function sayHi(name) {
  alert( `Hello, ${name}` );
}

sayHi JavaScript

sayHi("John"); // error!

let sayHi = function(name) {  // (*) no magic any more
  alert( `Hello, ${name}` );
};

(*)

age welcome()

let age = prompt("What is your age?", 18);

// 
if (age < 18) {

  function welcome() {
    alert("Hello!");
  }

} else {

  function welcome() {
    alert("Greetings!");
  }

}

// 
welcome(); // Error: welcome is not defined

let age = 16; //  16 

if (age < 18) {
  welcome();               // \   ()
                           //  |
  function welcome() {     //  |
    alert("Hello!");       //  |  
  }                        //  |
                           //  |
  welcome();               // /   ()

} else {

  function welcome() {
    alert("Greetings!");
  }
}

// 


welcome(); // Error: welcome is not defined

welcome if

welcome if

let age = prompt("What is your age?", 18);

let welcome;

if (age < 18) {

  welcome = function() {
    alert("Hello!");
  };

} else {

  welcome = function() {
    alert("Greetings!");
  };

}

welcome(); // 

?

let age = prompt("What is your age?", 18);

let welcome = (age < 18) ?
  function() { alert("Hello!"); } :
  function() { alert("Greetings!"); };

welcome(); // 

function f() {} let f = function() {}


Web Proxy Viewer  |  New URL  |  Original Page