您的位置:首页 > 移动开发 > Objective-C

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'));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: