日期:2014-05-16  浏览次数:20448 次

如何让for in循环只遍历对象的属性而不遍历方法
以下代码把对象实例的方法也遍历了,我的问题如题。
JScript code

function member(sName,sGender,iAge,sProfession){
    this.name = sName;
    this.sex = sGender;
    this.age = iAge;
    this.job = sProfession;
}
    member.prototype.showMemberInfo = function(obj){
        for(var i = 0 in obj){
                    alert(i + ": " + obj[i]);
        }
    }
var LDH = new member("刘德华","男",45,"艺人");
LDH.showMemberInfo(LDH);



------解决方案--------------------
if(typeof(obj[i])=="function"){
continue;
}
试试
------解决方案--------------------
加个typeof 类型判断

 for(var i = 0 in obj){
if(typeof(obj[i])!="function"){
alert(i + ": " + obj[i]);
}

}