25158

JS 对象及基本属性或方法

//使用构造函数构建对象 //1) //构造对象函数 var obj = new Object(); obj.name = "tom"; obj.age = 16; obj.gender = "male"; console.log(obj); //点访问符 console.log(obj.name); //中括号访问符 console.log(obj["gender"]); //2) //构造对象函数 var obj = {}; obj.name = "lucy"; obj.age = 18; obj.gender = "female"; console.log(obj); //对象序列化和返序列化 //将js对象转换为json字符串 var json = JSON.stringify(obj); console.log(obj); //将json字符串转换js对象 var obj = JSON.parse(json); console.log(obj); //3) //对象字面量 var obj = { name:"lily", age:15, gender:"male" }; console.log(obj); //删除属性 delete obj.name; console.log(obj); //4) //对象字面量 var obj = { name:"lisa", age:20, clazz:1903, QQ:123456, gender:"male" }; console.log(obj); //构造函数,谁创建了当前对象 var result1 = obj.constructor(); console.log(result1); //返回该对象的字符串描述信息 var result2 = obj.toString(); console.log(result2); //返回该对象的数字描述信息 var result3 = obj.valueOf(); console.log(result3); //增强for循环,遍历对象 for(var key in obj){ //获取属性值 var value = obj[key]; console.log(value); }

来源:博客园

作者:苯苯瓜

链接:https://www.cnblogs.com/lidyfamily/p/11426192.html

Recommend