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

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

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). Результатом будет следующий дамп данных:

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] }
...
Теги:
свойства, методы, объект
Добавлено: 22 Апреля 2018 14:21:09 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...