ifchang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # [Day04] 物件型別 ###### tags: `鐵人賽` # 物件 Object ### 除了基本型別其他都可以歸類至物件型別 建立一個自定義的物件主要有兩種方式,一個是透過 new 關鍵字來建立一個物件實體,再替此物件新增屬性與方法,另一個是**物件實字**(Object literal)直接用大括號 `{}`建立物件,並且在建立的同時直接指定屬性或方法。 * 建立函式(new 關鍵字) ```javascript= var person = new Object(); person.name = "Rex"; person.age = "25"; person.sayName = function() { alert( this.name ); }; ``` * 物件實字(Object literal) ```javascript= var person = { // 或直接指定屬性跟方法 name: "Rex", age: "25", 001: "abc-000", sayName: function() { alert( this.name ); } }; ``` 因為相對於建構式來說更為簡短,而且在易讀性跟效能都優於建構函式,所以普遍會比較建議使用物件實字。 而我覺得在新增屬性後面是加`,`的部分自己較容易搞錯,檢查時可多加留意。 ### 物件屬性存取 可以用透過`.`跟`[]`中括號來進行存取,兩者最大的不同是,當物件的索引鍵命名剛好是不符合 JS 的規則時,`[]`中括號可以正確存取。 ```javascript= person.name; // 'Rex' person.001; // 報錯 Uncaught SyntaxError: Unexpected number person["name"]; // 'Rex' person["001"]; // 'abc-000' ``` ### 物件屬性新增、刪除 新增屬性跟變數一樣直接用`=`,若想要刪除則透過 delete 關鍵字來刪除: ```javascript= var person = {}; person.name2 = "Tom"; person.name2; // "Tom" delete person.name2; // true person.name2; // 被刪除變 undefined ``` ### 判斷屬性是否存在 屬性不存在會回傳 undefined,如果想要檢查該屬性是否存在有三個方式 ```javascript= console.log(person.name); // 缺點是如果不存在或該屬性剛好是 undefined,則都會顯示 undefined person.hasOwnProperty("name"); // 只會檢查物件本身 console.log("name" in person); // in 運算子會繼續順著原型鏈上檢查 ``` # 陣列 Array JS 的陣列可視作一種特別的物件,沒有規定一定要放什麼東西,可以是原始資料類型、另一個陣列、物件甚至函數。 要注意的是,陣列是有順序性的集合,所以只能透過`[]`加上索引來存取。 跟物件一樣可以透過 * new 關鍵字來建立 ```javascript= var a = new Array(); a[0] = "Tom"; a[1] = "Mike"; a[2] = "Jerry"; a.length; // 3 ``` * 陣列實字的方式(實務上較常見的方式) ```javascript= var b = []; b[0] = "Tom"; b[1] = "Mike"; b[2] = "Jerry"; b.length; // 3 ``` 或者直接輸入值,不用再次輸入變數跟順序,各有利弊看情況使用 ```javascript= var c = ["Tom", "Mike", "jerry"]; c.length; // 3 ``` 陣列與字串一樣可以透過 length 屬性取得,而在陣列時 length 又可以被覆寫,如果原本 b.length = 3,覆寫成 b.length = 1, 後面的值就會被移除,如果覆寫成 b.length = 5,沒有的值會顯示 empty。 ![](https://i.imgur.com/T4la8hA.png) *b.length = 1, 剩下 Tom,Mike 跟 Jerry 被移除* ![](https://i.imgur.com/vcXniUn.png) *b.length = 5,沒有的值會顯示 empty* 參考資料: [卡斯柏 - JS 物件名詞解釋及常見觀念問題](https://wcc723.github.io/development/2020/09/21/js-object/) [008天重新認識JavaScript](https://www.tenlong.com.tw/products/9789864344130)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully