對象:
一矾芙、什么是對象:
生活中的一切事物都可以看成是對象,比如一臺電腦近上,一個人剔宪,一個杯子。每個對象都有屬于他自己的屬性壹无,比如人有性別身高和體重葱绒;電腦有品牌,杯子有顏色和容量等斗锭。
在js中地淀,對象就是屬性的集合。同時它也是一種數(shù)據(jù)類型
二岖是、創(chuàng)建一個對象demo01
????????????1.var obj={}
????????????2.var obj=new Object();
創(chuàng)建一個人的對象帮毁,并且為對象中添加屬性
????????????var person={
name:'張三',
age:18,
sex:'男'
????????????}
獲取對象中的屬性有兩種方法
1.對象名.屬性名
??????????????????person.name
2.對象名[屬性名]
??????????????????person[age]
三、對象中屬性的類型demo02
對象中的屬性可以是字符串豺撑,數(shù)字烈疚,也可以為函數(shù)。當(dāng)對象中的屬性值為函數(shù)的時候聪轿,我們稱這個屬性為對象的方法
????????????var person={
name:'張三',
age:18,
sex:'男',
sayName:function(){
console.log('我的名字叫張三')
}
????????????}
????????????person.sayName();
//對象中的方法想要訪問對象中的屬性要用this關(guān)鍵字
????????????var person={
name:'張三',
age:18,
sex:'男',
sayName:function(){
// console.log('我的名字叫'+name);name不會被輸出
//對象中的方法想要訪問對象中的屬性必須要用this關(guān)鍵字
console.log('我的名字叫'+this.name);
}
????????????}
????????????person.sayName();
//3.對象中的屬性被替換 ?demo03
????????????????var person={
name:'張三',
age:18,
sex:'男',
sayName:function(){
console.log('我的名字叫'+this.name);
}
????????????}
????????????????cat.sayName();
cat.name='李四';
????????????????console.log(cat.name);
????????????????cat.sayName();
四爷肝、給對象中的方法傳參demo04
??????????????var person={
name:'張三',
age:18,
sex:'男',
sayName:function(){
console.log('我的名字叫'+this.name);
},
count:function(a,b){
????????????????????console.log(a+b);
}
????????????}
????????????????cat.sayName();
????????????????cat.count(3,5);
五、如何遍歷js中的對象 ?demo05
????????????????var person={
name:'張三',
age:18,
sex:'男'
????????????????}
????????for(var key in person){
???????? //輸出對象中的屬性值
console.log(person[key]);
//輸出對象中的關(guān)鍵字
console.log(key);
????????}
知識點(diǎn)2:原始類型和引用類型
知識點(diǎn)3:遞歸函數(shù)