javascript之object

Object 是所有類的基礎類 

實例化:var obj = new Object();或者 var obj = {} ;


給對象設置屬性:

obj.name = '張3';
obj.age  = 20 ; 

也可以使用;obj["birthday"] = '1980-08-07';把屬性放在[" "]中。

給對象設置方法:

obj.say = function(){
alert('hello world!');
}


訪問對象的屬性或方法

alert(obj.name);//屬性

obj.say();//方法


delete 操作符 刪除對象的屬性或方法的

delete obj.age //刪除屬性;
delete obj.say ;//刪除方法


遍歷一個js對象  for in 語句式

for(var attribute in obj) {                   
alert(attribute +" : "+ obj[attribute]);  //訪問對象的屬性值使用[]
}                                             


Constructor保存對象的創建函數

alert(obj.constructor);

輸出:function Object(){[native code]}


hasOwnProperty(propertyName) 用於檢測給定屬性在對象中是否存在

alert(obj.hasOwnProperty('sex'));//如果obj裏面有sex,則返回true


檢測給定的屬性是否能被for in 所枚舉出來                      
alert(obj.propertyIsEnumerable('say'));      


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章