JavaScript 4 物件的操作

2021-09-22 18:30:53 字數 1341 閱讀 2363

物件的操作主要包括:訪問屬性、新增屬性、修改屬性、刪除屬性、遍歷屬性

一、訪問屬性

有2種方式:物件名.屬性名

、物件名[「

屬性名」]

**演示:

var person = 

console.log("姓名:"+person.name);//物件.屬性名

console.log("姓名:"+person["name"]);//物件[屬性名]

二、新增屬性

有2種方式:物件名.屬性名=值、物件名[「屬性名」]=值

**演示:

var person = 

person.height = 160;//物件.屬性名=值

person["weight"] = 45;//物件名["屬性名"]=值

console.log("身高:"+person.height+"cm,","體重:"+person.weight+"kg");

三、修改屬性

有2種方式:物件名.屬性名=值、物件名[「屬性名」]=值

**演示:

var person = 

person.name = "caifeng";//物件.屬性名=值

person["age"] = 25;//物件名["屬性名"]=值

console.log("姓名:"+person.name,",年齡:"+person.age);

四、刪除屬性

有2種方式:delete 物件名.屬性名、delete 物件名[「屬性名」]

**演示:

var person = 

delete person.name;// delete 物件名.屬性名

delete person["age"];//delete 物件名["屬性名"]

五、遍歷

有2種方式:for/in、object.keys(物件名)

1)for/in方式

**演示:

var person = 

for(var i in person)

2)object.keys(物件名)方式

**演示:

js物件4 物件的操作

使用typeof 方法 function isobject test else 原理 該方法傳入乙個變數,返回該變數型別所對應的字串 string number boolean undefined object null object function 對於null 陣列和物件,一律返回 objec...

學習4 物件轉殖

物件轉殖介面 param param from from param dsttype dsttype return t public static t cloneobject object from,classdsttype catch instantiationexception e catch ...

Javascript學習筆記 2(物件 函式)

1 原始變數 number string boolean null undefined。變數記憶體存的就是值,按值傳遞的 2 引用變數 物件變數,變數記憶體存的是指標,所以是按指標傳遞 鑑別原始型別 typeof 但typeof null object 鑑別引用型別 obj instaceof co...