grayshine
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # <font class="h2">型別是什麼?</font> ###### tags: `javascript` <style> .h2 { background: linear-gradient(135deg,#fff,#537479) ; color: #537479; display:block; padding: 6px 5px; border-radius: 4px; } .h3 { background: linear-gradient(180deg,#fff 50%,#c9d5d4) ; color: #537479; display:block; padding: 6px 5px; border-bottom: 3px solid #537479; } .h4 { color: #537479; font-weight:bold; font-size:1.1em; } </style> <br> 在javascript裡面,有許多種「資料型別(data type)」,但主要分兩大類,一種是==原始型別primitive type==,另一種是==物件Object==Primitive type以外的,例如array, function, map …etc。 ![](https://i.imgur.com/fwmaebr.png) ![](https://i.imgur.com/W9R8BMc.png) :::info 除了原始型別 (Primitive Type)外,還有物件型別 :bulb: javascript只有兩種型別:純值、物件 :bulb: 原始型別沒有屬性與方法,物件型別則可任意存取屬性與方法。 ::: :::danger null是屬於物件型別 ```javascript typeof null //Object ``` ::: <br> ### <font class="h3">原始型別 (Primitive Type)</font> 在JS裡,以下這六個原始型別(Primitive Type)皆屬於傳值。 - Boolean 布林值 - String 字串 - Number 數字 - Null 空值 - Undefined 未定義 - Symbol 符號 (ES6 新型別) `string`, `number`, `boolean` 合稱為「原始類型 (primitive type) 的值」;`null`, `undefined` 一般則稱為「特殊值」。至於 object 則稱為合成類型(complex type) 的值,object 是複雜的數據類型,又可分為3個子類型:`object`,` array`, `function`。 <br> ### <font class="h4">➤ 字串`String`</font> 例: ```javascript let a = 'hello'; let b = "你好嗎" ``` 字串外面必須包這單引號或雙引號 <br> ### <font class="h4">➤不是數值`NaN`</font> 將無法相加減乘除的類別做加減乘除,得出來的型別 ```javascript let total = myName * age; console.log(total)//NaN ``` :::danger :bulb:NaN是屬於number(數字型別) ::: <br> ### <font class="h4">➤ 布林`true` `false`</font> ```javascript let isWakeUp = false; console.log(isWakeUp); ``` 判斷對錯 ```javascript console.log(2>3); //結果為false ``` <br> ### <font class="h4">➤ undefined有定義尚未被賦予值</font> :::info ==JavaScript 變數的預設值為 undefined== ::: :::danger undefined 不能使用在 JSON 資料格式中,只能使用null定義空值 ::: `undefined` 代表的是「已經宣告卻沒有賦予任何的值」 ```javascript let a; console.log(a);//undefined,尚未被賦予值 ``` <br> ### <font class="h4">➤ not defined從未被定義</font> 取不到值為is not defined,not defined是從未被定義(會報錯),undefined是有定義沒有賦予值 :::info ```javascript console.log(typeof e);//查詢not defined的變數的型別,會是undefind ``` not defined 型別為undefined,這是typeof對not defined的一種保護措施 ::: <br> ### <font class="h4">➤ `null`</font> - `null`代表的是「「沒有值」 - 被告知為空值,可以<font color="red">清除暫存記憶體資料</font>,意思是「沒有值」的值。 例: ```javascript let c =1234; c= null; //c原本有植,可以下null指令清空它的資料 ``` :::info ==JavaScript 不會將預設值設定為 null,這是讓開發者用來定義「沒有值」的值(人為賦予的)== null型別為object,javascript一直以來的bug ::: :arrow_right:[undefined 與 null 的差異](https://hackmd.io/Zl5Y-jXmTUWfnuC-py35gg) <br> :::danger | | null | undefined | NaN | not defined | | ---- | ---- | --------- | --- | --- | | 型別| object | undefined |number|undefined| ::: <br><br> ### <font class="h4">➤ `Infinity`</font> 任何數除於0都會得到Infinity Infinity有正負值 ```javascript 100/0 //Infinity -100/0 //-Infinity !!(100/0) //true, Infinity是truthy值 ``` <br><br><br><br> ### <font class="h3">查詢型別`typeof`</font> ```javascript console.log(typeof 變數名稱); ``` :::info ==`typeof`會回傳字串== `typeof`屬於`undefined`型別 ::: <br><br><br><br> ### <font class="h3">原始型別的背後原理</font> JavaScript 為了節省系統資源,原始型別在使用時會自動裝箱(boxing)形成原始型別包裹物件,使用完畢後會自動拆箱(unboxing)還原回原始型別。 此外原始型別包裹物件則可用 valueOf 方法取得對應的原始型別。 ```javascript var str=new String("word"); var txt=str.substring(3); str=str.valueOf(); ``` :::info valueOf 方法最主要的目的,就是用來回傳特定物件相對應原始型別的值 ::: :::danger ==在數學運算時會優先呼叫.valueOf 方法,若回傳非原始型別則會改呼叫.toString 方法== ```javascript var n = new String('str') console.log(n+3) //"str3",這邊n會先執行valueOf n.valueOf = function(){ return{} //這邊刻意讓valueOf回傳為一個物件 } console.log(n+3) //"str3",這邊就會改用toString n.toString = function(){ return 5 } console.log(n+3) //這邊n+3就會變成8了 ``` ::: <br><br><br><br> ### <font class="h3">mutable(可變)與immutable objects(不可變)的不同</font> immutable objects不可變:原始型別(記憶體是不可改變) - 物件可以使用Object.assign創建一個新物件實現immutable ```javascript var obj = {name:'abc'} var obj2 = Object.assign({},obj,{age:30}) console.log(obj2 ) //{name: 'abc', age: 30} ``` <br><br> - 陣列可以使用slice實現immutable ```javascript var ary = [1,2,3] var ary2 = ary.slice() ary2.push(4) ``` ### <font class="h3">BigInt型別(超大整數型別)、Symbol型別</font> :arrow_right: [BigInt型別(超大整數型別)](https://hackmd.io/@grayshine/rkQQUixws) Symbol型別是ES6新增的型別 ==Symbol無法被列舉(for in、Object.key)出來== ```javascript var isRead = Symbol() var obj = { name:'John', age:30 } obj[isRead] = true Object.keys(obj) //['name', 'age'] ```

    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