or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
基礎 JavaScript - 物件特性,位元運算
tags:
Tag(JavaScript)
JavaScript 的概念
目的性及用意
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Node.js 是一個能執行 JavaScript 的環境
執行方法:
直接執行
node
,再輸入你的程式- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →一個物件可以是個零至多種屬性的集合,而屬性是鍵 (key) 與值 (value) 之間的關聯。 一個屬性的「值」可以是某個基本型別,也可以是另一個物件,甚至可以是一個函數。物件可以是瀏覽器預先定義好的,當然也可以是由自己定義物件的屬性與內容。
分為基本型別 與物件型別
null 是基本型別之一,但 typeof null 卻得到 object,而非 null!這可說是一個 bug,可是若因為修正了這個 bug 則可能會導致很多網站壞掉,因此就不修了!
建立物件的方式
直接用大括號
{ }
,即可建立起一個新的物件,並且在建立物件的同時直接指定屬性至該物件內如何存取物件
物件本身的組成是由一個
{屬性(property) / 值(value)}
組成的,可以透過 JavaScript 的規則定義一個物件的名稱Property 的 value 可以是任何的型態值,包括 string 、 integer 、 function 又或是另一個 Object
使用 Object 中的 Property 方式有兩種
.
來進行存取[ ]
來進行存取判斷屬性是否存在
in
運算子 與hasOwnProperty()
雖然兩者都可以檢查物件的屬性是否存在,但 hasOwnProperty() 方法不會往上檢查物件的原型鏈 (prototype chain),只會檢查物件本身是否存在這個屬性,而 in 運算子則會繼續往物件原型鏈上檢查
如果要判斷一個變數是否為 Object 可以使用
typeof
但是要注意,除了真正的 Object 外, null 、 Array 也都會回傳 Object ,因此在判斷時,記得先處理掉變數型態可能是 null 或Array 的可能性
運算子
+
、=
、==
都是運算子,本身這也是屬於函式的一種,是用來將它本身 前後方的值 做計算,然後回傳一個新的值方向及優先性
寫 JavaScript 時也是習慣由左到右撰寫,但其實 JavaScript 文法並不是只有由左到右,而是依據 結合性 (Associativity) 決定它是由左至右,還是由右至左閱讀
除此之外,JavaScript 還有一個優先性,高優先性 (Precedence) 的運算子會被優先執行
=
:「得到或指定至」,把右邊運算結果傳至左邊(賦值)==
:「等於」,進行類型轉換,較寬鬆(判斷相等,不判斷型態)===
:「嚴格等於」,不進行類型轉換,較嚴格(判斷相等,型態也相同)!==
:不等於&&
:and||
:or!
:not- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →對複合值的比對使用的是參考記憶體位址
在 JavaScript 中,物件是比較記憶體位置
短路特性(邏輯運算)
javascript 裡面只要是 0、""、null、false、undefined、NaN 都會被判定為 false
邏輯與 && 的運算方式
邏輯與 || 的運算方式
位元位移
<<
:將位元往左移一位,可將其看成乘以 2 的幾次方10 << 2 = 10 * 2^2 = 40
上面的 2^2,是表示 2 的 2 平方
>>
:將位元往右移一位,可將其看成除以 2 的幾次方。若不能整除會直接捨去1024 >> 2 = 1024 / 2^2
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →26
的二進制表示法為011010
、37
的二進制表示法為10010
&&
為邏輯運算,而&
為位元運算,||
為邏輯運算,而|
為位元運算&(and)
當成位元間的 &&,以 0011 & 1001 ( 3 & 9 ) 為例,會回傳 0001( 1 )
可以拆解成
&
1 效果如同 n%
2 → 1 , 代表 n 的最後 1 個位元數 ( 2⁰ ) 是 1, n 為 奇數&
1 效果如同 n%
2 → 0 , 代表 n 的最後 1 個位元數 ( 2⁰ ) 是 0,n 為 偶數(|)OR
當成位元間的
||
,以0011
|
1001
(3
|
9) 為例,會回傳
1011(
11 )可以拆解成
^
位元XOR
(EXCLUSIVE OR)其中一個位元為 1 而另一個為 0 時輸出才是 1
指派運算子 (Assignment Operator)