手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >js 遍历对象的属性的代码
js 遍历对象的属性的代码
摘要:如:复制代码代码如下:Function.prototype.addMethod=function(methodName,func){if(!...

如:

复制代码 代码如下:

Function.prototype.addMethod=function(methodName,func){

if(!this.prototype[methodName]){

this.prototype[methodName]=func;//给原型增加方法,此方法会影响到该类型的实例上

}

return this.prototype;//返回原型,此类型实例可以进行链形调用

}

function CustomObject(name,value){

this.name=name || 'CustomeObject';

this.value=value || 0;

this.toString=function(){

return '[name:'+this.name+',value:'+this.value+']'

}

}

CustomObject.addMethod('testFun',function(){})

var obj=new CustomObject();

var info='';

for(var property in obj){

info+=property+" | ";

}

alert(info); // name | value | toString | testFun |

但此时for in 也把该对象所继承于prototype对象中的属性也遍历出来了。如果要剔除它所继承的属性,可以用hasOwnProperty语句。如

复制代码 代码如下:

Function.prototype.addMethod=function(methodName,func){

if(!this.prototype[methodName]){

this.prototype[methodName]=func;//给原型增加方法,此方法会影响到该类型的实例上

}

return this.prototype;//返回原型,此类型实例可以进行链形调用

}

function CustomObject(name,value){

this.name=name || 'CustomeObject';

this.value=value || 0;

this.toString=function(){

return '[name:'+this.name+',value:'+this.value+']'

}

}

CustomObject.addMethod('testFun',function(){})

var obj=new CustomObject();

var info='';

for(var property in obj){

if(!obj.hasOwnProperty(property)) continue;

info+=property+" | ";

}

alert(info); // name | value | toString |

【js 遍历对象的属性的代码】相关文章:

JavaScript Date对象详解

一些很实用且必用的小脚本代码第1/5页

网页常用特效代码整理

一段实时更新的时间代码

jQuery 遍历函数详解

js跨域请求的5中解决方式

Ctrl + Enter提交前检测的代码

些很实用且必用的小脚本代码

document对象execCommand的command参数介绍

超酷右下浮出广告窗口代码

精品推荐
分类导航