javascript之object

来源:互联网 发布:爱奇艺 视频缓存 mac 编辑:程序博客网 时间:2024/05/29 09:12

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'));      


0 0
原创粉丝点击