Как вывести все свойства и методы объекта в JavaScript.


Напишем на JavaScript небольшую функцию, которая позволит вывести все свойства и методы объекта.

1
2
3
4
5
6
7
8
function allProperties(obj, objName){
    var result = "";
    for (var i in obj) //на каждом шаге обращаемся к свойствам объекта по индексу
        result += objName + "." + i + " = " + obj[i] + "<br />\n";
    return result;
}
result = allProperties(Roles, 'Roles');
document.write(result);

В примере выше происходит вызов функции и в качестве параметров ей передается объект (коллекция MongoDB) Roles (используется в контексте MVC-фреймворка Sails для node.js). Результатом будет следующий дамп данных:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Roles.attributes = [object Object]
Roles.identity = roles
Roles.globalId = Roles
Roles.subscribe = function () { [native code] }
Roles.unsubscribe = function () { [native code] }
Roles.introduce = function () { [native code] }
Roles.publish = function () { [native code] }
Roles.pluralize = function () { [native code] }
Roles.room = function () { [native code] }
Roles.classRoom = function () { [native code] }
Roles.subscribers = function () { [native code] }
Roles.module = sails-mongo
Roles.url = mongodb://localhost:27017/sails
Roles.adapter = sails-mongo
Roles.migrate = alter
Roles.globalize = true
Roles.autoPK = true
Roles.autoUpdatedAt = true
Roles.autoCreatedAt = true
Roles.__destroy = function () { [native code] }
Roles.__save = function () { [native code] }
Roles.alter = function () { [native code] }
Roles.count = function () { [native code] }
Roles.create = function () { [native code] }
Roles.createEach = function () { [native code] }
Roles.define = function () { [native code] }
Roles.describe = function () { [native code] }
Roles.destroy = function () { [native code] }
Roles.drop = function () { [native code] }
Roles.find = function () { [native code] }
Roles.findAll = function () { [native code] }
Roles.findOrCreate = function () { [native code] }
Roles.findOrCreateEach = function () { [native code] }
Roles.getAutoIncrementAttribute = function () { [native code] }
Roles.join = function () { [native code] }
Roles.registerCollection = function () { [native code] }
Roles.stream = function () { [native code] }
Roles.teardown = function () { [native code] }
Roles.transaction = function () { [native code] }
Roles.update = function () { [native code] }
Roles.updateAll = function () { [native code] }
Roles.generateDynamicFinder = function () { [native code] }
Roles.schema = [object Object]
Roles.findByRolename = function () { [native code] }
Roles.findByRolenameIn = function () { [native code] }
Roles.findByRolenameLike = function () { [native code] }
Roles.findAllByRolename = function () { [native code] }
Roles.findAllByRolenameIn = function () { [native code] }
Roles.findAllByRolenameLike = function () { [native code] }
...

Другие посты

Категория: JavaScript для сайта

Комментарии (1)

 

  1. Иван:

    Часто объекты многомерны для этого лучше подойдет эта функция:

    function strObj(obj,prefix,depth) {
    var str = «\r\n\r\n\r\n\r\n\r\n\r\n»;
    for(k in obj) {
    str += prefix+» «+k+»: «+ obj[k]+»\r\n»;
    if(obj[k] && ‘object’ === typeof obj[k] && prefix.length < depth-1) {
    str += strObj(obj[k],prefix+"-",depth)
    }
    }
    return str;
    }

    // Пример
    var t = {"name":"sdf",
    "login":11,
    "fdsf":{"ee":"ee1","22":"11"}
    };
    alert(strObj(t,"",2)); // 10 – максимальная глубина

Оставить комментарий


Яндекс.Метрика