let obj = { name: 'cnplaman', age: 18 } obj.gender = 'male'; obj.marriage = true; obj.age = 24; console.log(obj.name); console.log(obj['name']); console.log(obj);
delete obj.marriage; console.log(obj);
let sql = 'age' in obj console.log(sql);
.通过对象Object的静态方法:entries()、keys()或values()间接获取;这三个方法返回入口、键和值的数组
console.log(Object.keys(obj).length); console.log(Object.values(obj).length); console.log(Object.entries(obj).length);
// 遍历的是key,要想获取值value,则配合对象使用 for (item in obj) { console.log(item, obj[item]); } //fail // obj.forEach(item => { // console.log(item); // }) // fail:TypeError: obj is not iterable // for (item of obj) { // console.log(item); // } //也可以先entries()、keys()或values(),再使用数组遍历方法,如for()或forEach() Object.keys(obj).forEach(item => { console.log(item); }) Object.values(obj).forEach(item => { console.log(item); }) Object.entries(obj).forEach((item) => { console.log(item); }) //解构再输出 Object.entries(obj).forEach(([value, key]) => { console.log(key, value); })
function setObj(obj) { obj.name = 'glpla'; } var stu = new Object(); setObj(stu); console.log(stu.name);
import { createApp } from 'vue' import { computed, onMounted, reactive, ref } from 'vue'; const { add, dec } = require('./module/test')
let obj = { name: 'cnplaman', age: 18, address: { city: 'gz', code: 541000 } }
let { name, age } = obj
let { name: uname, age: uage } = obj
let { name: uname, age: uage, gender = 'male' } = obj
let { name, age, address: { city } } = obj
let { name, age, address: { code: postcode } } = obj