tingtinghsu
    • 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
      • Invitee
    • 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
    • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
Invitee
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
--- title: javascript 6.11 ES6 (ES2015) tags: javascript, 5x --- 6.11 0326 javascript ES6 (ES2015) === ES6有很多比ES5更加好用的語法! # let 變數 在ES5,使用`var`宣告變數 在ES6,使用`let`宣告變數 javascript ```javascript //var name = "kitty"; //在ES5,用var宣告變數 let name = "kitty"; //在ES6,用let宣告變數 console.log(name); ``` console ```bash "kitty" undefined ``` ## `let` 與`var`相比的好處 ### 1. let * 區域變數:被設成`let`的變數僅能作用在宣告的範圍裡面 (如下圖例:為`迴圈`內部) ```javascript= for (let i = 0; i < 3; i++ ) { console.log(i); } ``` console ```javascript 0 1 2 ``` * `let`如果未事前先宣告,就會出現錯誤 ```javascript= for (let i = 0; i < 3; i++ ) { } console.log(i); ``` console ```javascript "error" "ReferenceError: i is not defined at nororos.js:7:38 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" ``` ### 2. var 為全域變數,商業邏輯出錯時不容易debug ```javascript= for (var i = 0; i < 3; i++ ) { } console.log(i); ``` 等於 ```javascript= var i; for (i = 0; i < 3; i++ ) { } console.log(i); ``` 結果皆為: console ```javascript 3 ``` ### 3. `let`和`var`同時使用,變數會被全域的`var`決定 javascript ```javascript= var i; //全域 for (let i = 0; i < 3; i++ ) { } console.log(i); ``` console ```javascript undefined //而不是3 ``` ### 4. `let` 與`var`不同之處:變數的有效範圍不同 設定為`let`的變數,就算在迴圈內數值被改變,也只會作用在區域範圍內,不會影響到外面 ```javascript= let i = 100; for (let i = 0; i < 3; i++) { console.log('hi'); } console.log(i); ``` console ```javascript "hi" "hi" "hi" 100 //仍然是100 ``` 設定為`var`的變數,在任何區域重新給值時,數值都會被改變 ```javascript= var i = 100; for (var i = 0; i < 3; i++) { console.log('hi'); } console.log(i); ``` console ```javascript "hi" "hi" "hi" 3 //100被改為3 ``` ### ES6 寫法 ```javascript= let i = 10; for (let i = 0; i < 5; i++) { console.log('hi'); } console.log(i); ``` console ```javascript "hi" "hi" "hi" "hi" "hi" 10 // 迴圈印5次hi,let變數仍為10 ``` ### ES5寫法 ```javascript= var i = 10; for (var i = 0; i < 5; i++) { console.log('hi'); } console.log(i); ``` console ```javascript "hi" "hi" "hi" "hi" "hi" 5 // 迴圈印5次hi,var變數被改為5 ``` # const 常數 常數不可被更改 ```javascript= const age = 18; console.log(age); //印出18 age = 20; //TypeError: 型別錯誤,不可指定變數值給常數 const age = 20; //SyntaxError: 變數名稱已經被宣告過了 ``` console ```javascript 18 "error" "TypeError: Assignment to constant variable. at nororos.js:5:5 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" "error" "SyntaxError: Identifier 'age' has already been declared 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" ``` # 解構 ## ES6 `let { key, key }` = `變數名稱` javascript ```javascript= var hero = { name: '孫悟空', age: '18' }; let { name, age } = hero; // 從var指定為let變數 console.log(name); console.log(age); ``` console ```bash "孫悟空" "18" ``` # 字串與變數的串接 ## ES6 使用` `` ` 圍住整體,`${變數}`帶到字串裡 javascript ```javascript= let name = "kitty"; console.log(`Hello, ${name}`); //"Hello, kitty" ``` ## ES5 使用`+` 串接字串與變數 javascript ```javascript= var name = "kitty"; console.log("Hello, " + name); //"Hello, kitty" ``` console ```bash "Hello, kitty" ``` # 箭頭函式 e.g. 假設有一html語法如下: ```htmlmixed <a href="#" id="zone">Hello Zone</a> ``` ## ES6 以箭頭`=>`代替函式`function()` javascript ```javascript= document.querySelector('.zone').addEventListener('click', evt => { console.log('我被按了!'); }); ``` function內的程式碼如果只有一行,可以連大括弧都省略 ```javascript= document.querySelector('.zone').addEventListener('click', evt => console.log('我被按了!');); ``` ## ES5 函式`function()` javascript ```javascript= document.querySelector('.zone').addEventListener('click', function(evt){ console.log('我被按了!'); }); ``` # 陣列 ```javascript= var friends = ['Ross','Joey','Chandler','Monica','Phoebe','Rachel']; ``` ## ES6 `forEach()` javascript ```javascript= friends.forEach(function(x){ console.log(x); }); ``` 用箭頭函數改寫 ```javascript= friends.forEach( x => { console.log(x); }); ``` fuction內的程式碼只有一行,所以可以省略大括弧,變得更精簡 ```javascript friends.forEach(x => console.log(x)); ``` ## ES5 `for迴圈` javascript ```javascript= for (var i = 0; i < friends.length; i++){ console.log(friends[i]); } ``` console ```bash "Ross" "Joey" "Chandler" "Monica" "Phoebe" "Rachel" ``` # 物件簡寫 ## ES6 key與value名稱相同,value可省略 若物件的key與value的名稱設定相同,可只寫一次 ```javascript= function profile(firstName, lastName, age){ return { firstName, lastName, age, fullName: function() { return lastName + firstName; } } } let p1 = profile('悟空', '孫', 18); console.log(p1.fullName()); ``` ## ES5 ```javascript= function profile(firstName, lastName, age){ return { firstName: firstName, lastName: lastName, age: age, fullName: function() { return lastName + firstName; } } } var p1 = profile('悟空', '孫', 18); console.log(p1.fullName()); ``` console ```bash "孫悟空" ``` # 物件導向的寫法 ## ES6 `class-base` 類別裡面可寫`constructor`建構子和一般的function * 這裡的class是`Syntax Sugar語法糖衣` ```javascript= class Pokemon { constructor(name, skill) { this.name = name; this.skill = skill; } attack(){ console.log('攻擊!'); } } var pikachu = new Pokemon('皮卡丘','電'); console.log(pikachu); console.log(pikachu.attack()); //"攻擊!" undefined pikachu.attack(); //"攻擊!" ``` console ```bash [object Object] { name: "皮卡丘", skill: "電" } "攻擊!" undefined "攻擊!" ``` ## ES5 `prototype-base` javascript ```javascript= function Pokemon(name, skill) { this.name = name; this.skill = skill; } Pokemon.prototype.attack = function() { var sound = this.name.slice(0,2).repeat(2); console.log("攻擊吧!" + this.name + "!使出" + this.skill + "~" + sound); } var pikachu = new Pokemon("皮卡丘", "十萬伏特"); pikachu.attack(); ``` console ```bash "攻擊吧!皮卡丘!使出十萬伏特~皮卡皮卡" ``` # 其他ES6常用語法 ## map ## reduce ## filter ## some ## find ## every # ES6的瀏覽器支援程度 可用轉譯套件轉成較低版本的javascript

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