function sayHelloTo(customer) {
console.log('Hi! ' + customer);
}
sayHelloTo('孫小美');
sayHelloTo('錢夫人');
sayHelloTo('阿土伯');
輸出:
"Hi! 孫小美"
"Hi! 錢夫人"
"Hi! 阿土伯"
fuction名字: sayHelloTo
代入參數: customer
function裡面:做的事(跟客人打招呼)
function sayHelloTo(customer) {
console.log('Hi! ' + customer);
}
就算有SOP,仍必須主動做出跟客人打招呼
這件事:
sayHelloTo('客人的名字');
/*執行定義好的function,並代入字串*/
eg. 檢查年齡
function isAdult(age) {
if(age >= 18) {
console.log(age+'歲,可以喝酒了!');
}
else
console.log(age+'歲,好年輕呀!');
}
isAdult(17);
isAdult(18);
輸出:
"17歲,好年輕呀!"
"18歲,可以喝酒了!"
var age = prompt('請輸入年齡');
function isAdult(age) {
if(age >= 18) {
console.log(age+'歲,可以喝酒~');
}
else
console.log(age+'歲,好年輕呀!');
}
isAdult(age);
輸出
"15歲,好年輕呀!"
"33歲,可以喝酒~"
因為console.log功能不包含回傳,
(只負責印出內容,但是回傳值是undefined)
所以我們寫一個function,用true明確的回傳結果。
/*function + 包含true / false 條件判斷先寫好*/
function isAdult(age) {
if(age >= 18) {
return true;
}
else {
return false;
}
}
/*if true,條件判斷*/
var yourAge = 18;
if (isAdult(yourAge)) { /*如果為True*/
console.log(yourAge+'歲,可以喝酒了!');
}
else {
console.log(yourAge+'歲,好年輕呀!');
}
/*function + 包含true / false 條件判斷先寫好*/
function isAdult(age) {
if(age >= 18) {
return true;
}
else {
return false;
}
}
/*if true,條件判斷*/
var yourAge = prompt('請輸入你的年紀:');
if (isAdult(yourAge)) { /*如果為True*/
console.log(yourAge+'歲,可以喝酒了!');
}
else {
console.log(yourAge+'歲,好年輕呀!');
}
用console的方式,雖然能夠在螢幕上顯示出來,但沒有回傳值。
> console.log(`hi`)
hi
< undefined
> function abc() { return 100 }
< undefined
> abc ()
< 100
function isAdult(age) {
if(age >= 18) {
return true;
}
else {
return false;
}
}
等於
function isAdult(age) {
if(true) {
return true;
} /*true重複,多餘邏輯*/
else {
return false;
}
}
等於
function isAdult(age) {
return (age >= 18);
}
function isAdult(age) {
return (age >= 18);
}
改為沒有名字的function:
var isAdult = function(age) {
return (age >= 18);
function a1() {
console.log('a1');
}
a2 = function() {
console.log('a2');
}
a1();
a2();
輸出:
"a1"
"a2"
javascript會自動做變數提升,用var設定變數,
在一些情況下會呼叫不到。
/* var abc, a2; java根據下面程式設定abc與a2變數*/
function a1() {
console.log('a1');
}
a2 = function() {
console.log('a2');
}
a1();
a2();
var abc = 123;
如果把a1()
, a2()
提到前面:
a1();
// javascript會把變數提升 => var a2; // undefined
a2();
var abc, a2;
function a1() {
console.log('a1');
}
a2 = function() {
console.log('a2');
}
var abc = 123;
執行時會出現問題
"a1"
"error"
"TypeError: a2 is not a function
at delobel.js:2:1
at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13924
at https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10866"
hello是function,
hello()是執行函數的結果。
function hello() {}
undefined
function hello() {console.log('hi');}
undefined
> hello()
hi VM150:1
undefined
> hello
ƒ hello() {console.log('hi');}
function hello_return() {return 1};
undefined
hello_return();
1
hello_return;
ƒ hello_return() {return 1}
function bmiResult(height, weight) {
//
}
解法:
function bmiResult(height, weight) {
bmi = weight / ( height * height )
return bmi;
}
var height = prompt('請輸入你的身高(公尺):');
var weight = prompt('請輸入你的體重(公斤):');
console.log( bmiResult(height, weight) );
function isLeapYear(year) {
}
解法:
function isLeapYear(year) {
if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)){
return true;
}
else
return false;
}
var year = prompt('請輸入年份');
if (isLeapYear(year)) {
console.log( year +'年是閏年');
}
else {
console.log( year +'年不是閏年');
}
輸出:
"1980年是閏年"
"1990年不是閏年"
"2000年是閏年"
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing