javascript之object
2016-05-22 00:00
447 查看
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'));
实例化: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'));
相关文章推荐
- Objective-C ---点语法 (梳理整理)
- Objective-C ---代理和协议 (梳理整理)
- ObjectC Hook函数的实现与实战
- NSObject的方法说明
- 讲的很详细的一篇关于object equals() & hashCode() 的文章
- 使用cx_freeze工具打包时提示 AttributeError 'module' object has no attribute '_fix_up_module':
- Object Following for UAV(无人机物体跟随)
- iOS多线程之NSThread、NSOperationQueue、NSObject和GCD的区别
- Volley(2)—JsonObjectRequest和ImageRequest
- spring的 object is not an instance of declaring class
- 不管在不在arc下,object对象都有强引用、弱引用之分
- Xcode:duplicate declaration of method 'XXX'
- 重写Object类中的equals方法
- Programming with Objective-C(七)
- Objective-C Coding Style Guidelines
- scala学习13之与trait同名的object对象
- objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects
- SpringMVC数据格式转换报错:rejected value [065966] error in object 'command' on field 'imgNumber'
- ObjectInputStream序列化反序列化
- for(Object obj : objs) 解释