Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got the following problem.

I have different object like:

user with the method : id, username, firstname, lastname
article :with some other method;

i want to add dynamic method like getById, getByUsername etc.

var object = new Object() ; // Object can take the value user, article or other. </code>
for(var key in object) {
this["getBy" + key]= function(args) {  
    var result = query("SELECT * FROM " + object.name + ' WHERE ' key + "=" +args);
    console.log(key); //always return the last key of object
    return(result);
}

// then

var user = new User();
user.getByUsername('someone');

The dynamic add of method like this works fine when there is no argument in the function. All the method are well define but when I call them key as change (of course) to I just have the last function.

If someone can help me, I spend all my day on this, and I still can't find a solution.

share|improve this question
 
could you precise how you call the functions ? –  Newben 16 hours ago
 
btw, could you provide details about the user object ? –  Newben 16 hours ago
 
Your question seems not clear because your code is incomplete, what this refers to ? user object, but intuively, what seems to happen is that each key in your loop must override its precedent –  Newben 16 hours ago
 
As is written, this in your code currently refer to the global (or in node, the module global) object. Or it may even refer to null in the case of ES5 strict mode. Since this in javascript depends on how the surrounding code gets executed you need to provide the code for that as well - is it a constructor? A callback? An event handler? –  slebetman 16 hours ago
 
your approach of SELECT is difficult and not maintainable, usually there are standard methods on an User,Article as entities of a database, you build more difficult SELECT by building a query and not building a method. –  GeoPhoenix 10 hours ago

1 Answer

var User = function(id, username, firstname, lastname) {
               this.name = 'user';
               this.id = id; 
               this.username = username;
               this.firstname = firstname; 
               this. lastname = lastname;
           };



var Find = function(objectName){

               var object = new objectName();

               // I have a first method all 
               // this refers to find object
               this.all = connection.query("SELECT * FROM " + objectName);


               //then I want to add ByX method, X taking the values id, username ...

               for( key in object ) {
                   if(key != "name") { // I exclude name method of user

                       var query = "SELECT * FROM user WHERE " + key + "=" ; 

                       this['By'+key] =  function(args) {
                           query += args;
                           return(connection.query(query);
                      };

               }



// then In my code

var find = new Find('User');
var list = find.ByX(something);
    list
        .on("error", function(err){
            console.log(err);
        })
        .on('result' function(row){
            console.log(row);
        })
   ;

when the code is compiled object Find have methods ByX, but when I run find.ByX(something);

the key passed to last value it occurs to be lastname. and it's run

var query = "SELECT * FROM user WHERE " + key + "=" ;
function(args) { 
        query += args; // query is recalculated and key is now lastname;
        ...
}

I did it in this way somewhere else in my code but it seems because the function is waiting for args value, the query is not set.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.