var myDog = { "name": "marcel", "legs": 4, "tails": 1, "friends": ["clause"] };
.
),一個是中括號操做符([]
)。var testObj = { "hat": "ballcap", "shirt": "jersey", "shoes": "cleats" }; var hatValue1 = testObj.hat; // . 訪問 var hatValue2 = testObj["hat"]; // [] 訪問 testObj.handbag = "guchi"; //增長屬性 delete testObj.handbag; //刪除屬性
var myObj = { gift: "pony", pet: "kitten", bed: "sleigh" }; function checkObj(checkProp) { // Your Code Here if (myObj.hasOwnProperty(checkProp)){ return myObj[checkProp]; } else{ return "Not Found"; } } checkObj("gift");
for循環中的三個表達式用分號隔開:javascript
for ([初始化]; [條件判斷]; [計數器])
java
初始化
語句只會在執行循環開始以前執行一次。它一般用於定義和設置你的循環變量。正則表達式
條件判斷
語句會在每一輪循環的開始執行,只要條件判斷爲 true
就會繼續執行循環。當條件爲 false
的時候,循環將中止執行。這意味着,若是條件在一開始就爲 false
,這個循環將不會執行。express
計數器
是在每一輪循環結束時執行,一般用於遞增或遞減。數組
var ourArray = []; for (var i = 0; i < 5; i++) { ourArray.push(i); }
Math.random()
用來生成一個在0(包括0)到1(不包括1)之間的隨機小數,所以Math.random()
可能返回0但毫不會返回1。dom
指定區間[min,max)生成隨機整數函數
Math.floor(Math.random() * (max - min + 1)) + min
Regular expressions
正則表達式被用來根據某種匹配模式來尋找strings
中的某些單詞。spa
舉例:若是咱們想要找到字符串The dog chased the cat
中單詞 the
,咱們可使用下面的正則表達式: /the/gi
code
咱們能夠把這個正則表達式分紅幾段:對象
/
是這個正則表達式的頭部
the
是咱們想要匹配的模式
/
是這個正則表達式的尾部
g
表明着 global
(全局),意味着返回全部的匹配而不單單是第一個。
i
表明着忽略大小寫,意思是當咱們尋找匹配的字符串的時候忽略掉字母的大小寫。
// Setup var testString = "Ada Lovelace and Charles Babbage designed the first \ computer and the software that would have run on it."; var expressionToGetSoftware = /software/gi; var softwareCount = testString.match(expressionToGetSoftware).length; //match函數返回的是匹配對象的列表
特殊選擇器:/\d+/g
,這個正則表達式匹配一個或更多數字
/\s+/g
,(小寫s)選擇一個字符串中的空白,空白字符有 " "
(空格符)、\r
(回車符)、\n
(換行符)、\t
(製表符) 和 \f
(換頁符)
\S
(大寫S)匹配任何非空白字符