0

I am trying to set some variable to an object. But i want it to be set like the code below is there a way to set a variable like this?

myObject = {};

myObject.save = function (var) {
  console.log(var);
}

myObject.users.save(); // must output 'users';
myObject.fruits.save(); // output fruit;

the save method is just an example method. it means that i need to chain a method on the variable string.

i trying to achieve something like that. any ideas how to achieve that? thanks

1
  • No there's not, you can't use properties that don't exists, and you don't have a users or fruits property, and if you did, the save method would still have nothing to do with those properties. Commented Mar 16, 2014 at 16:11

2 Answers 2

1

You can create a List class for your users and fruits lists, and provide regular list methods like add, remove, size and etc. to make it actually like a list class, and define your save method as a prototype method:

var List = (function(){

    function List(listType){
        this.listType = listType;
        this._list = [];
    }

    List.prototype.add = function(item){
        this._list.push(item);
    };

    List.prototype.size = function(item){
        return this._list.length;
    };

    List.prototype.save = function(item){
        console.log(this.listType);
    };

    return List;

})();

then you can use it in your object like:

var myObject = {};
myObject.users = new List("users");
myObject.fruits  = new List("fruits");

now you can actually call those two lines of code:

myObject.users.save(); // output 'users';
myObject.fruits.save(); // output 'fruits';

and you can also define a save method for myObject and actually call the lists save method:

myObject.save = function(listType){
    if(myObject[listType] instanceof List){
        myObject[listType].save();
    }
    else{
        console.log("there is no such a list named : " + listType);
    }
};

and call it like:

myObject.save("users");
myObject.save("fruits");
Sign up to request clarification or add additional context in comments.

Comments

0

You can do like

var obj = {
    users: {
        save: function () {
            console.log('Users');
        }
    },

    fruits: {
        save: function () {
            console.log('Fruits');
        }
    },
}

(or)

var obj = {};
obj.users = {};
obj.users.save = function () {
            console.log('Users');
        };
obj.fruits = {};
obj.fruits.save = function () {
            console.log('Fruits');
        };

3 Comments

problem is i never know what to propery will be.
May be, you can explain the context so we can understand the problem clearly.
I building document storage system and for clean user friendly api. i want it like mongodb store method: db.users.save({name: 'somebody'});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.