iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
自我挑戰組

Be friend with JavaScript系列 第 5

Day 5 - Object & Function

函式(Function)

function 是物件的一種,查看 function 的資料型態時結果會顯示 object。
定義一個函式包含三個部份,依次為:

  1. 函式的名稱。
  2. 在括號 ( ) 中放入參數,有很多個參數時會用逗號隔開。
  3. 在大括號 { } 中,放入定義函式功能的 JavaScript 程式碼。
    而定義函式之後並不會馬上執行,要透過呼叫函式來執行。
    例如:

無參數的 function

// 定義函式
function sayHi(){
    console.log("Hi!");
}
// 呼叫函式
sayHi(); // Hi!

有參數的 function

// 定義函式
function sayHi(name){
    console.log("Hi!" + name);
}
// 呼叫函式
sayHi("Lily"); // Hi!Lily

return

  • 如果函式有回傳值,要用 return 回傳結果,且要用一個變數把回傳的內容接住來儲存,否則會出現 undefined。
    (當然如果函示沒有回傳值時,就不需要使用 return)
    例如:
function count(a, b){
  console.log(a + b);
}
count(3,5); // 8
console.log(count(3,5)); // undefined

console.log(count(3, 5)) 會出現 undefined 的結果,是因為在 function 裡面只是使用 console.log 把值印出來,並沒有用 return,所以沒有回傳值。
改善的方式如下:

function count(a, b){
  return(a + b);
}
let add = count(3,5);
console.log(add); // 8
  • 許多 function 都有 return value,例如 prompt() , push()...等
let friends=["John","May","Mike"];
let returnWord = friends.push("Mary");
console.log(returnWord); // 4

push() return 的東西是新的陣列長度,所以上面的程式碼回傳 4

  • return 也可以用來中斷函式執行
    當函式執行到 return 時,就會終止這個函式後續的動作,執行到 return 即停止。

補充:

  • Default Parameter 預設值
    假如在 function 中應填入兩個參數,但我們卻只有填一個時,如果對他們做計算會得到 NaN 的結果,例如:
function count(a, b){
    console.log(a * b);
}
count(5) // NaN

這時候可以在括號中放入預設值避免這種情況發生

function count(a = 1, b = 3){
    console.log(a * b);
}
count(5) // 15
// 因為只填了 1 個參數,第 2 個參數預設值為 3,所以結果為 5 * 3 = 15 
count(3,6) // 18
  • IIFE 立即函式
    當有大量程式碼時不用一一想變數名稱來呼叫函式,只要把函式包在括號裡就可以立即執行
(function sayHi(){
    console.log("Hi")
})()

// Hi

如果要放參數的話直接在括號裡放入即可

(function sayHi(name){
    console.log("Hi "+name)
})("Mary");

// Hi Mary

物件(Object)

物件 (object) 是一個複合資料型態 (composite data type),用 {} 裝資料,物件包含屬性(property)和方法(methods),物件的屬性值如果是一個函式,我們稱它是物件的方法 (method)。

物件宣告的方式

  1. Object Constructor
let obj = new Object();
  1. Object Literal
let obj = {};

取得物件屬性的方法

  1. []
  2. .(dot notation)

例如:

//property
let Jay = {
    name : "Jay",
    age:30
};
console.log(Jay.age); // 30
console.log(Jay["age"]) // 30

執行物件裡的方法(可以定義一個物件可以做的動作)

//property
let Jay = {
    name : "Jay",
    age:30,
//method
    walk(){
        console.log("Jay is walking on the street.");
    },
    say(word){
        console.log("Jay says " + word)
    }
};
Jay.walk(); // Jay is walking on the street.
Jay.say("Hello!"); // Jay says Hello!

在物件裡新增資料

可以用 . 賦值,也可以用 [] 賦值

let home={};
home["motherName"] = "Mary";
home["fatherName"] = "John";
home.dogs = 1;
home.children = 3;
console.log(home) // {motherName:"Mary",fatherName:"John",dogs:1,children:3}

修改物件

let home={
	motherName:"Mary",
	fatherName:"John",
	dogs:1,
	children:3
};
home.motherName = "Janet"; // 重新賦予值
home.dogs += 2; //也等於 home.dogs=home.dogs+2
console.log(home); // {motherName:"Janet",fatherName:"John",dogs:3,children:3}

刪除物件

let home={
	motherName:"Mary",
	fatherName:"John",
	dogs:1,
	children:3
};
delete home.dogs;
console.log(home) // {motherName:"Janet",fatherName:"John",children:3}
delete home.motherName;
console.log(home) // {fatherName:"John",children:3}
console.log(home.motherName) // undefined (已經刪除了找不到資料)

補充:
JavaScript 內建的物件有 Number, Math, Date, String, Array, RegExp...等,且每個都有自己的屬性與方法。

Math 和 Date 的詳細介紹在 Day 29 - Math object & Date object

參考資料:
MDN - 函式
JavaScript Object


上一篇
Day 4 - Array
下一篇
Day 6 - Loop
系列文
Be friend with JavaScript39
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言