array 用 [ ]
object 用 { }
寫法:
var a = [
"1",
"2",
"3"
];
//換行不換行都可以
也可以這樣寫:var a = new Array("1", "2", "3");
a[0]
取第一個陣列值a[-1]
取最後一個陣列值
shift()
移動const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
unshift()
添加新元素const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
push()
後增
pop()
後刪
shift()
前刪
unshift()
前增
更多函式使用可以來W3Schools查看:JavaScript Array
物件.屬性 = 值
物件.方法(參數)
相同物件都有相同的屬性,不同的值;也會有相同方法
object={key1:value, key2:value, key3:value}
範例:
var name = {
firstName: "Banana",
lastName: "Liao",
fullName: function(){
return this.firstName + " " + this.lastName
}
}
console.log(member.fullName())
//實作結果: "Banana Liao"
typeof "Banana" //"string"
typeof 15 //"number"
typeof true //"boolean"
typeof x //"undefined"
typeof {name:'Banana', age:22} //"object"
typeof [1,2,3] //"object"
typeof null //"object"
typeof function func(){} //"function(不是object)"