当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js显示对象所有属性和方法的函数

js显示对象所有属性和方法的函数

2019年01月30日  | 移动技术网IT编程  | 我要评论
function showobjproperty2( obj ) { 
    // 用来保存所有的属性名称和值 
    var attributes = '' ; 
    var methods = ''
    // 开始遍历 
    for ( var p in obj ){ 
        // 方法 
        if ( typeof( obj[p] ) === "function" ){ 
            attributes += '属性:' + p + '\r\n'
            // obj[p](); 
        } else { 
            // p 为属性名称,obj[p]为对应属性的值 
            methods += '方法:' + p + " = " + obj[p] + "\r\n" ; 
        } 
    } 
    // 最后显示所有的属性 
    return attributes, methods
}
function showobjproperty1(obj) 
{ 
    var attributes = ''; 
    var methods = ''; 
    for(const attr in obj){ 
        if(obj.attr != null) 
            attributes = attributes + attr + ' 属性: ' + obj.i + '\r\n' ; 
    else 
        methods = methods + '方法: ' + attr + '\r\n'; 
    } 
    return attributes, methods 
}     

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网