第一部分 基础知识
第4章 对象
- 数组跟对象的区别:a). 位置索引;b). 对象中可包含JS内置函数
- 创建对象的基本语法:var cat = {
leg: 4,
name: “Harmony”,
color: “brown”
}
对象键的写法:键名称有空格必须用双引号,对象键名称无空格(骆驼写法)可以无双引号 - 访问对象中的值可像访问数组元素那样使用方括号或 . (点)加位置索引
- 数组元素的位置索引是数字
- 对象元素的位置索引是键(字符串)
程序举例18 (不建议)
//Show me the property of the cat
var cat = { leg: 4, name: “Harmony”, color: “brown” }
cat [“name”]
返回值:”Harmony”
程序举例19
//Show me the property of the cat
var cat = { leg: 4, name: “Harmony”, color: “brown” }
cat. leg
返回值:4
程序解释19
- 使用点符号时,键中不能包含任何特殊字符(例如空格)
- 当需要查找对象所有的“键”名称时,可以使用JavaScript内置函数 Object. keys ( )
程序举例19
//Show me the keys of the cat
var cat = { leg: 4, name: “Harmony”, color: “brown” }
Object. keys ( cat )
返回值: ["leg", "name", “color"]
- 给对象添加元素时,可以像给数组添加元素那样使用方括号,但方括号中填写的不是数字,而是字符串(必须要用双引号)
- 对象新添加的键不会像数组那样会有“正确的”“按顺序的”位置索引(因为对象的索引是字符串)
程序举例20 (不建议)
//Add a new property to the cat
cat [ “face” ] = “cute”
console. log (cat)
返回值:{leg: 4, name: "Harmony", color: "brown", face: "cute"}
- 给对象添加元素时,也可以用点符号添加(键名称不能有特殊符号)
程序举例21
//Add a new property to the cat
cat. size = “small”
console. log (cat)
返回值:{leg: 4, name: "Harmony", color: "brown", face: "cute", size: “small"}
- 数组和对象可以互相包含
程序举例22
//Draw a family tree
var family = [
{ name: "David", },
{ name: "Fanny", },
{ name: "Kellen", },
{ name: "Winnie", },
]
family [0].child = family [3]
console.log (family [0])
返回值:name: “David”, child: “Winnie”
程序举例23
//Make a load record
var ownedMoney = {}
ownedMoney. Jimmy = 5
ownedMoney. Anna =7
ownedMoney. Jimmy
返回值:5
ownedMoney. Jimmy +=3
返回值:8
程序举例24
//Make a movie list
var movies = {
"Finding Nemo" : {
releaseDate: 2003,
duration: 100,
cast: ["Albert Brooks", "Ellen DeGeneres", "Alexander Gould"],
format: "DVD"
},
"Star War": {
releaseDate: 1983,
duration: 134,
cast: ["Mark Hamill", "Harrison Ford", "Carrie Fisher"],
format: "DVD"
},
"Harry Potter" : {
releaseDate: 2005,
duration: 157,
cast: ["Daniel Radcliffe", "Emma Watson", "Rupert Grint"],
format: "blue-ray"
}
}
//Find keys of the movie list
Object.keys (movies)
返回值:[“Finding Nemo", "Star War", "Harry Potter"]
//Find keys of “Finding Nemo”
Object.keys (movies ["Finding Nemo"])
返回值:[“releaseDate", "duration", "format"]
//Find the cast of “Star War”
Object.assign (movies ["Star War”]["cast"]) 或
Object.assign (movies ["Star War"].cast)
返回值:[“Mark Hamill", "Harrison Ford", "Carrie Fisher"]
程序解释24
- 由于cast是数组(里面的内容应为值),所以不能用访问keys的方式获取,要用. assign进行访问
程序举例25
//Make a movie list
function addMovies (movieName, releaseDate, leadingRole, format){
var movieList = {}
var movieSearch = []
if(movieName == null){
console.log ("Error! Please enter a correct movie name.")
return 1
}
if(movieSearch.indexOf(movieName) == -1){
movieSearch.push (movieName)
movieList [movieName] = {}
movieList [movieName].releaseDate = releaseDate
movieList [movieName].leadingRole = leadingRole
movieList [movieName].format = format
return 0
}
}
程序举例26
//Make a score record for your friends. Key = Friend’s name; Value = Friend’s score
var score = {}
function mark (name, number){
if(name == null || number == null){
console.log ("Please enter correct information.")
return 1
}
if(score [name] == null) {
score [name] = number
return 0
}
if(score [name] != null) {
score [name] += number
return 2
}
}
程序举例27
//Get the element of 123 from the var below
var myCrazyObject = {
"name": "A ridiculous object",
"some array": [7,9, {purpose: "confusion", number: 123},3.3],
"random animal": "Banana Shark"
}
myCrazyObject ["some array”][2].number
返回值:123