javascript之Object类型属性、方法

来源:互联网 发布:偶像活动真食玩淘宝店 编辑:程序博客网 时间:2024/05/28 03:03
<html><head><title>005</title><script type="text/javascript" charset="utf-8">//Object类是所有类的基础类。/*//var obj=new Object();var obj={};//也相当于创建一个对象,实例化对象。//obj.name='张三';obj.age=20;obj.sex='男';obj.say=function(){alert('hello world');}//alert(obj.name);//张三//alert(obj.age);//20//obj.say();//hello world//删除一个对象的属性。//delete操作符, 删除对象的属性或方法。delete obj.age;delete obj.say;alert(obj.name);//张三alert(obj.age);//undefinedalert(obj.sex);//男obj.say();//什么都没有,在控制台上会显示报错。*///如何去遍历一个js对象 for in 语句式var obj={};obj.name='张三';obj.age=20;obj.sex='男';obj['birthday']='1980-08-07';//这样也可以为给对象设置属性,但是要放上双引号或是单引号。obj.say=function(){alert('hello world');}/*for (var attribute in obj){alert(attribute+":"+obj[attribute]);//这里是js的一个特性,不能用obj.attribute}//name:张三  age:20  sex:男  say: obj.say=function(){alert('hello world');}*///Constructor保存对象的创建函数/*alert(obj.constructor);//function Object() { [native code] }var arr=[];alert(arr.constructor);//function Array() { [native code] }*///hasOwnProperty(propertyName)方法 用于检测给定属性在对象中是否存在。//alert(obj.hasOwnProperty('name'));//true//isPrototypeOf(Object)这个方法检测原型,暂时先不讲,等学到原型再讲。//propertyIsEnumerable(propertyName):用来判断给定的属性是否能用for in 循环来枚举。alert(obj.propertyIsEnumerable('name'));//true,因为之前都已经枚举过了。/*alert(obj.propertyIsEnumerable('say'));//true*///toLocaleString():返回对象的字符串表示,该字符串与执行环境的地区对应。//toString :返回对象的字符串表示。//valueOf :返回对象的字符串、数值或是布尔表示。
//定义对象,千万要记住这样定义对象,各属性之间千万要用逗号隔开,而不是分号。//person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};</script></head><body></body></html>

原创粉丝点击