@GivenCui
2016-05-27T05:45:04.000000Z
字数 3291
阅读 818
js高级
JS的字面量对象, 就是最简单化的创建对象的方式, 和用构造函数创建的对象一样存在于堆内存中.
语法 { 属性1 : 属性值 , 属性2 : 属性值, .... }
属性访问
1. 通过" . " 语法访问 eg. obj.propertyName
2. 通过" [ ] " 语法访问 eg. obj["propertyName"]
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>创建字面量对象</title><script type = "text/javascript">// 创建字面量对象// 里面可以是任意类型// 数组中也可以放对象var person = {name : "周大生",age : 18,isMarried : false,dog : {name : "Nimo",age : 0.2},friends : [{name : "杜甫",age : 18},{name : "诺顿",age : 500},{name : "乔布斯",age : 30},{name : "霍金",age : 48}]};// 获得"Nimo"这个属性值console.log(person.dog.name);// 获得"杜甫"这个属性值console.log(person.friends[0].name);</script></head><body></body></html>
用 for-in 方法
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>遍历字面量对象</title></head><body><script type="text/javascript">// 创建字面量对象// 里面可以是任意类型// 数组中也可以放对象var person = {name : "周大生",age : 18,isMarried : false,dog : {name : "Nimo",age : 0.2},friends : [{name : "杜甫",age : 18},{name : "诺顿",age : 500},{name : "乔布斯",age : 30},{name : "霍金",age : 48}]};// 遍历自变量对象// tempPro是每个属性的名字// 想得到friends中的每个对象的名字和年龄for (var tempPro in person) {// console.log( tempPro);// console.log(person[tempPro]);if (tempPro === 'friends'){// 遍历friends数组for (var index in person[tempPro]) {console.log("我的朋友是" + person[tempPro][index].name + ",年龄是"+ person[tempPro][index].age);}}}</script></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>字面量对象的实例(创建与遍历)</title></head><body><script type="text/javascript">// 分析字面量对象// 字面量对象也可以搭建一种树形结构// 假数据,用来测试系统的//分析字面量对象var rootObj = {//地区数组regions : [//地区对象{regionName : "北京地区",//学校数组schools : [{schoolName : "北京一中",//班级数组classes : [{className : "H5-1604",//学生数组students : [{studentName : "丁丁"},{studentName : "马丹丹"}]},{className : "iOS-1602",//学生数组students : [{studentName : "丁环宇"},{studentName : "张俊"}]}]},{schoolName : "北京二中",//班级数组classes : [{className : "H5-1605",//学生数组students : [{studentName : "刘升升"},{studentName : "张三三"}]},{className : "iOS-1603",//学生数组students : [{studentName : "丁丁"},{studentName : "肖小军"}]}]}]}]}// 找到丁丁同学,并且打印其相关信息// 得到地区数组var regions = rootObj.regions;// 遍历地区数组for (var tempRegionIdx in regions) {//得到每个地区对象var tempRegion = regions[tempRegionIdx];//得到学校数组var schools = tempRegion.schools;// 遍历学校数组for (var tempSchoolsIdx in schools) {// 得到每个学校对象var tempSchool = schools[tempSchoolsIdx];// 得到班级数组var classes = tempSchool.classes;// 遍历班级数组for ( var tempClassIdx in classes) {// 得到每一个班级对象Var tempClass = classes[tempClassIdx];// 得到学生数组var students = tempClass.students;// 遍历学生数组for(var tempStudentIdx in students){// 得到每一个学生对象var tempStudent = students[tempSchoolsIdx];// 判断要找的学生if(tempStudent.name === '学生test'){console.log("\n 地区 : "+tempRegion.name + "\n 学校 : " + tempSchool.name + "\n 班级 :" + "\n 学生 : " + tempStudent.name);break;}}}}}</script></body></html>
数据模型 : 我们会把JS中的字面量对象进行数据解析,解析后的数据会存储在相应的数据模型中,数据模型就是JS中的通过相应构造函数创建出来的一个个对象.
/**************封装数据模型***************/// 最后把地区对象存到数组中// 地区的构造函数function Region (regionName) {this.regionName = regionName;// 学校数组this.schools = new Array();}// 学校的构造函数'function School (schoolName) {this.schoolName = schoolName;// 班级数组this.classes = new Array();}// 班级的构造函数function Class (className) {this.className = className;// 学生数组this.students = new Array();}// 学生的构造函数function Student (studentName) {this.studentName = studentName;}/********************************************///在解析了*每一个对象*(用for-in遍历)后在相应的信息存入对应的对象// 每一个json字面量对象 ====> new一个实例对象// json字面量对象不是真正的对象概念,只是一种数据结构而已.