Node.js – Object 物件

測試環境為 CentOS 8 x86_64

Node.js – Object 物件通常包含了數個變數(在物件中被稱為屬性 properties)及數個函式(函數 Function).

Node.JS 所需套件須透過 npm 來下載,所以必須安裝 npm (Node Package Manager) 套件.

[root@localhost ~]# yum install -y nodejs
[root@localhost ~]# yum install -y npm

檢視一下 Node.JS 與 npm 版本.

[root@localhost ~]# node -v
v10.23.1
[root@localhost ~]# npm -v
6.14.10

自定一個 Object 物件,範例參考 https://developer.mozilla.org/zh-TW/docs/Learn/JavaScript/Objects/Basics

[root@localhost ~]# vi object.js
var person = {
  name : ['Bob', 'Smith'],
  age : 32,
  gender : 'male',
  interests : ['music', 'skiing'],
  bio : function() {
    console.log(this.name[0] + ' ' + this.name[1] + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
  },
  greeting: function() {
    console.log('Hi! I\'m ' + this.name[0] + '.');
  }
};
console.log(person.name[0] + ' ' + person.name[1] + ' is ' + person.age + ' years old. He likes ' + person.interests[0] + ' and ' + person.interests[1] + '.');
person.bio();
person.age=45;
person.bio();
person['age']=26;
person.bio();
person.greeting();

執行結果.

[root@localhost ~]# node object.js 
Bob Smith is 32 years old. He likes music and skiing.
Bob Smith is 32 years old. He likes music and skiing.
Bob Smith is 45 years old. He likes music and skiing.
Bob Smith is 26 years old. He likes music and skiing.
Hi! I'm Bob.

程式說明.

var person = {
  name : ['Bob', 'Smith'],
  age : 32,
  gender : 'male',
  interests : ['music', 'skiing'],
  bio : function() {
    console.log(this.name[0] + ' ' + this.name[1] + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
  },
  greeting: function() {
    console.log('Hi! I\'m ' + this.name[0] + '.');
  }
};

以上是物件內容,它包含了.

  • 屬性(Properties) :
    name , age , gender 及 interests 等.
  • 函數(Function) :
    bio 與 greeting.
  • 內建函數 console.log
    console.log(`Server running at http://${hostname}:${port}/`);
    

    console.log([data][, …args]) ,Console 模組提供了一個簡單的 Debugging 資訊在標準輸出並加上換行 newline 來顯示.

  • this
    裡面用到 this.name[0] , this.name[1] , this.interests[0] ,this.interests[1] ,其中的 this 代表目前程式碼的物件,以剛剛範例就等於 person .
console.log(person.name[0] + ' ' + person.name[1] + ' is ' + person.age + ' years old. He likes ' + person.interests[0] + ' and ' + person.interests[1] + '.');
person.bio();
person.age=45;
person.bio();
person['age']=26;
person.bio();
person.greeting();

我們透過幾種方式來使用 person 物件.

  • console.log(person.name[0] + ‘ ‘ + person.name[1] + ‘ is ‘ + person.age + ‘ years old. He likes ‘ + person.interests[0] + ‘ and ‘ + person.interests[1] + ‘.’);
    直接使用 name , age , gender 及 interests 等屬性(Properties),並透過 Node.js 內建函數 console.log 將資訊顯示在標準輸出.
    執行結果:

    Bob Smith is 32 years old. He likes music and skiing.
    
  • person.bio();
    呼叫 person 物件的 bio 函數.
    執行結果:

    Bob Smith is 32 years old. He likes music and skiing.
    
  • person.age=45;
    person.bio();
    先設定 age 屬性值(使用 Dot notation),再呼叫 person 物件的 bio 函數.
    執行結果:

    Bob Smith is 45 years old. He likes music and skiing.
    
  • person[‘age’]=26;
    person.bio();
    先設定 age 屬性值(使用 Bracket notation) ,再呼叫 person 物件的 bio 函數.
    執行結果:

    Bob Smith is 26 years old. He likes music and skiing.
    
  • person.greeting();
    直接呼叫 person 物件的 greeting 函數.
    執行結果:

    Hi! I'm Bob.
    

通常 object 會寫到另外一個檔案(模組),請參考 – http://benjr.tw/103466 .

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料