欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-03 17:43:15
function ShowObjProperty2( obj ) { // 用来保存所有的属性名称和值 var attributes = '' ; var methods = '' // 开始遍历 for ( var p in obj ){ // 方法 if ( typeof( obj[p] ) =... ......
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 
}