Ruofan Wei
    • 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
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 基礎 JavaScript - 物件特性,位元運算 ###### tags: `Tag(JavaScript)` ###### JavaScript 的概念 - 是一種物件導向的「腳本」語言 - JavaScript 是用來改進 Web 瀏覽器的客戶端體驗 ###### 目的性及用意 - 改善網站性能(歸功於 Ajax) - 修復瀏覽器缺陷(CSS 較新特性的支援) - 用於行動裝置 - 將一些處理從伺服端改到客戶端,降低伺服器負載 >[參考資料](http://test.domojyun.net/MEMO/JavaScript/) JavaScript 基本認識 ## :memo: Node.js Node.js 是一個==能執行 JavaScript 的環境== ### 執行方法: 直接執行 `node`,再輸入你的程式 ![](https://i.imgur.com/3ZXfw0E.png) ## :memo: javascript 物件 >[參考資料](https://medium.com/enjoy-life-enjoy-coding/javascript-%E9%97%9C%E6%96%BC-object-%E4%B8%80%E5%8F%A3%E6%B0%A3%E5%85%A8%E8%AA%AA%E5%AE%8C-4bb924bcc79f) 關於 Object ,一口氣全說完 ##### 一個物件可以是個零至多種屬性的集合,而屬性是鍵 (key) 與值 (value) 之間的關聯。 一個屬性的「值」可以是某個基本型別,也可以是另一個物件,甚至可以是一個函數。物件可以是瀏覽器預先定義好的,當然也可以是由自己定義物件的屬性與內容。 分為基本型別 與物件型別 - [x] 基本型別:數字、字串、布林、null、undefined - [x] 物件型別:任何非基本型別的值皆為物件 ###### null 是基本型別之一,但 typeof null 卻得到 object,而非 null!這可說是一個 bug,可是若因為修正了這個 bug 則可能會導致很多網站壞掉,因此就不修了! #### 建立物件的方式 直接用大括號 `{ }`,即可建立起一個新的物件,並且在建立物件的同時直接指定屬性至該物件內 ```javascript= var person = { name: 'Kuro', job: 'Front-end developer', sayName: function() { alert( this.name ); } }; ``` #### 如何存取物件 ##### 物件本身的組成是由一個 `{屬性(property) / 值(value)}` 組成的,可以透過 JavaScript 的規則定義一個物件的名稱 Property 的 value 可以是任何的型態值,包括 string 、 integer 、 function 又或是另一個 Object :::success ##### 使用 Object 中的 Property 方式有兩種 1. 物件的屬性可以透過` .` 來進行存取 2. 或是可以透過` [ ] `來進行存取 ```javascript= //使用 . 點號運算子 console.log(mary.name) //印出 Mary //使用 [] 中刮號運算子 const propertyNameA = 'say' const propertyNameB = 'Hello' mary[propertyNameA + propertyNameB]() //取出 function 後執行,印出 Hello Mary ``` ##### 判斷屬性是否存在 `in` 運算子 與 `hasOwnProperty()` ```javascript= var obj = { name: 'Object' }; // 透過 in 檢查屬性 console.log( 'name' in obj ); // true console.log( 'value' in obj ); // false // 透過 hasOwnProperty() 方法檢查 obj.hasOwnProperty('name'); // true obj.hasOwnProperty('value'); // false ``` `雖然兩者都可以檢查物件的屬性是否存在,但 hasOwnProperty() 方法不會往上檢查物件的原型鏈 (prototype chain),只會檢查物件本身是否存在這個屬性,而 in 運算子則會繼續往物件原型鏈上檢查` ::: ##### 如果要判斷一個變數是否為 Object 可以使用 `typeof ` 但是要注意,除了真正的 Object 外, null 、 Array 也都會回傳 Object ,因此在判斷時,記得先處理掉變數型態可能是 null 或Array 的可能性 ```javascript= typeof 'Hello World!'; // 'string' typeof true; // 'boolean' typeof 1234567; // 'number' typeof null; // 'object' typeof undefined; // 'undefined' typeof { name: 'Jack' }; // 'object' typeof Symbol(); // 'symbol' typeof function() {}; // 'function' typeof [1, 2, 3]; // 'object' typeof NaN; // 'number' ``` ### 運算子 >[參考資料](https://wcc723.github.io/javascript/2017/12/11/javascript-grammar/) JavaScript 的文法學 ###### `+`、`=`、`==` 都是運算子,本身這也是屬於函式的一種,是用來將它本身 ==前後方的值== 做計算,然後回傳一個新的值 :::info ##### 方向及優先性 寫 JavaScript 時也是習慣由左到右撰寫,但其實 JavaScript 文法並不是只有由左到右,而是依據 結合性 (Associativity) 決定它是由左至右,還是由右至左閱讀 ```javascript= // 將 5 的數值賦予給 b ,再由 b 賦予給 a,所以 a 的值會得到 5 a = b = 5; console.log(a); ``` 除此之外,JavaScript 還有一個優先性,==高優先性 (Precedence) 的運算子會被優先執行== ```javascript= 3 + 3 * 3 // 得到 12,因為 * 的優先值高於 + (3 + 3) * 3 // 得到 18,因為 () 內的優先計算 ``` ::: - `=` :「得到或指定至」,把右邊運算結果傳至左邊(==賦值==) - `==`:「等於」,進行類型轉換,較寬鬆(==判斷相等,不判斷型態==) - `===` :「嚴格等於」,不進行類型轉換,較嚴格(==判斷相等,型態也相同==) - `!==`:不等於 - `&&`:and ```javascript= var a1 = true && true; // t && t 回傳 true var a2 = true && false; // t && f 回傳 false var a3 = false && true; // f && t 回傳 false var a4 = false && (3 == 4); // f && f 回傳 false var a5 = "Cat" && "Dog"; // t && t 回傳 Dog var a6 = false && "Cat"; // f && t 回傳 false var a7 = "Cat" && false; // t && f 回傳 false ``` - `||`:or ```javascript= var o1 = true || true; // t || t 回傳 true var o2 = false || true; // f || t 回傳 true var o3 = true || false; // t || f 回傳 true var o4 = false || (3 == 4); // f || f 回傳 false var o5 = 'Cat' || 'Dog'; // t || t 回傳 Cat var o6 = false || 'Cat'; // f || t 回傳 Cat var o7 = 'Cat' || false; // t || f 回傳 Cat ``` - `!`:not ## :memo: 物件特性 >[參考資料](https://kennyliblog.nctu.me/2019/06/17/Javascript-basic/#Handling-Events) Javascript 基礎 對複合值的比對使用的是參考記憶體位址 在 JavaScript 中,==物件是比較記憶體位置== ```javascript= var a = { name: "eason", action: "haha" }; var b = { name: "eason", action: "haha" }; console.log(a === b); //false ``` ### 短路特性(邏輯運算) >[參考資料](https://ithelp.ithome.com.tw/articles/10191343) 重新認識 JavaScript ##### ` javascript 裡面只要是 0、""、null、false、undefined、NaN 都會被判定為 false` 邏輯與 && 的運算方式 ```javascript= var a = 5 && 6; console.log(a); //返回的結果為 6 ``` ```javascript= var a = false && 6; console.log(a); //返回的結果為 false ``` 邏輯與 || 的運算方式 ```javascript= var a = false || 6; console.log(a); //返回的結果為6 ``` ```javascript= var a = true || 6; console.log(a); //返回的結果為true ``` ### 位元位移 1. `<<`:將位元往左移一位,可將其看成乘以 2 的幾次方 10 << 2 = 10 * 2^2 = 40 上面的 2^2,是表示 2 的 2 平方 2. `>>`:將位元往右移一位,可將其看成除以 2 的幾次方。若不能整除會直接捨去 1024 >> 2 = 1024 / 2^2 ![](https://i.imgur.com/5ME5HW8.png) ## :memo: 位元運算 AND (&)、OR(|)、NOT(!)、XOR(^) ![](https://i.imgur.com/xUAqh7I.png) `26` 的二進制表示法為 `011010` 、 `37` 的二進制表示法為 `10010` :::warning `&&` 為邏輯運算,而 `&` 為位元運算,`||` 為邏輯運算,而 `|` 為位元運算 ::: ### `&(and)` ![](https://i.imgur.com/8YiWRoK.png) 當成位元間的 &&,以 0011 & 1001 ( 3 & 9 ) 為例,會回傳 0001( 1 ) 可以拆解成 ![](https://i.imgur.com/qysB0c7.png) - n `&` 1 效果如同 n `%` 2 → 1 , 代表 n 的最後 1 個位元數 ( 2⁰ ) 是 1, n 為 奇數 - n `&` 1 效果如同 n `% `2 → 0 , 代表 n 的最後 1 個位元數 ( 2⁰ ) 是 0,n 為 偶數 ![](https://i.imgur.com/2CIEJdp.png) ### `(|)OR` ![](https://i.imgur.com/8rvqgSw.png) 當成位元間的 `||`,以 `0011` `|` `1001` ( `3` `|` 9` ) 為例,會回傳 `1011` ( `11 ) 可以拆解成 ![](https://i.imgur.com/piBYV1U.png) ### `^` 位元 `XOR` (EXCLUSIVE OR) ##### 其中一個位元為 1 而另一個為 0 時輸出才是 1 ![](https://i.imgur.com/p5T6pJs.png) ![](https://i.imgur.com/UyZ48x1.png) ``` js AND運算: 0 AND 0 0 0 AND 1 0 1 AND 0 0 1 AND 1 1 OR運算: 0 OR 0 0 0 OR 1 1 1 OR 0 1 1 OR 1 1 XOR運算: 0 XOR 0 0 0 XOR 1 1 1 XOR 0 1 1 XOR 1 0 NOT運算: NOT 0 1 NOT 1 0 ``` #### 指派運算子 (Assignment Operator) ![](https://i.imgur.com/Gl6aaNx.png)

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