JavaScriptオブジェクトのプロパティ

900 ワード

var person={
username:'king',
age:12,
salary:123,
addr:'shanghai'
sex:'nan'
};

//    

console.log(person.username);
//          []
var key='username';
console.log(person.key); //undefined
console.log(person[key]); //king
console.log(person['key']);  //undefined
function PersonInfo(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
//             ,    this          
}
var person1=new PersonInfo('king',34,'nan');
console.log(person1.name);

//    

var obj={};//   
obj.username='king';
obj.age=23;
obj['test']='hahha haha';

//      

obj.username='xiaohong';

//    

delete obj.test;

//for in     

var obj1={
x:1,
y:2
};
for(var p in obj1){
console.log(p+'
'); } // var obj2={ username:'qw', sayHi:function(){ return 'haha'; } };