无为清净楼资源网 Design By www.qnjia.com
如:
复制代码 代码如下:
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 |
标签:
遍历对象,属性

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com