最基本点运算
let person = { name: 'henry', age: 18, run: function() { console.log('running'); } } person.run();
属性的读取:
let person = {
name: 'henry',
age: 18
}
console.log(person.name);
console.log(person['name']);
//二者输出均为henry
其中方括号里的值可以是个变量:
let person = { name: 'henry', age: 18 } let variable = 'name'; console.log(person[variable]); variable = 'age'; console.log(person[variable]);
读取对象中的对象属性--点运算:
let person = { name: 'henry', age: 18, parents: { papa: 'jack', mama: 'mary' } } console.log(person.parents.papa); console.log(person['parents']['mama']);
//输出jack
mary
属性的赋值和读取:
let person = { name: 'henry', age: 18 } person.name = 'tom'; person['age'] = 10 console.log(person.name); console.log(person.age);
//输出tom
10
属性的查看(使用Object.keys方法)
let person = {
name: 'henry',
age: 18
}
console.log(Object.keys(person));
//输出一个数组['name','age']
属性的删除(delete):
let person = {
name: 'henry',
age: 18
}
delete person.name;
console.log(person);
增加:
let person = {
name: 'henry',
age: 18
}
person.gender = 'male'
标签:console,name,自定义,对象,age,person,let,log,属性 From: https://www.cnblogs.com/ppch-max/p/16837478.html