如何创建具有属性和具有相同名称的函数的类

时间:2015-09-02 16:45:03

标签: javascript class

我希望能够在javascript中同时调用类似的内容:

classInstance.room.get('criteria');
classInstance.room('criteria').remove('criteria');
classInstance.room().update('criteria');

我见过在shouldjs

上实现了类似的功能
should(10).be.a.Number();
(10).should.be.a.Number();

更新

我有以下代码:

function connectToDatabase() {
    var server = orientDB(dbConfig.server);
    var db = server.use(dbConfig.database);
    db.on("endQuery", function onDbEndQuery() {
        db.server.close();
    });
    return db;
}
var DbSet = function DbSet(name) {
return {
    list: function list(where, select, order) {
        where = where || true;
        select = _.isString(select) || _.isArray(select) ? select : '*';
        order = _.isString(order) || _.isArray(order) ? order : 'rid';
        return connectToDatabase()
            .select(select)
            .from(name)
            .where(where)
            .order(order)
            .all();
    },

    get: function get(where, select) {
        where = where || true;
        select = _.isString(select) || _.isArray(select) ? select : '*';
        return connectToDatabase()
            .select(select)
            .from(name)
            .where(where)
            .all()
            .then(function onResults(results) {
                if (results.length > 1) {
                    throw new Error('multiple results');
                }
                return results[0];
            });
    },
    create: function create(record) {
        return connectToDatabase()
            .insert()
            .into(name)
            .set(record)
            .one();
    },

    update: function update(where, changes) {
        where = where || true;
        return connectToDatabase()
            .update(name)
            .set(changes)
            .where(where)
            .scalar();
    },

    remove: function remove(where) {
        where = where || true;
        return connectToDatabase()
            .delete('VERTEX', name)
            .where(where)
            .scalar();
    }
};
};
var db = function getDb() {
    return {
        room: new DbSet('Room'),
        invitation: new DbSet('Invitation'),
        participant: new DbSet('Participant'),
    };
};
module.exports = db();

我想更改代码能够执行以下代码:

var db=require('path/to/database');
var room = db.room.get({name:'room 1'});
var sameRoom = db.room({name:'room 1'}).get();
db.room.create({name:'second room'});
db.room({name:'second room'}).create();

//same for methods list and delete
var room = db.room.list({status:'active'});
var sameRooms = db.room({status:'active'}).list();
db.room.update({name:'second room'},{status:'inactive'});
db.room({name:'second room'}).update({status:'inactive'});

我希望能够为邀请和参与者执行相同的代码。

1 个答案:

答案 0 :(得分:1)

我们需要有关这些功能的更多信息,但此代码提供了这些功能。

Klass = function () {};
Klass.prototype.room = function () {
    ....
    return {
        get: function () {...},
        remove: function () {...},
        update: function () {...}
    }
};
Klass.prototype.room.get = function () {...};

classInstance = new Klass();