0

I'm doing a code for forms, as have a lot of forms need to simplify the way to mask fields in the forms.For I made a class that encapsulates the ids of the fields and assign specific interactions for each.

I have code:

var Mask=(function() {

var me=this;    

var eventInterator;
var func; 
var fields;

function Mask(fd,ev,fn){

  this.setFunction(fd);
  this.setFunction(fn);
  this.setEvent(ev);
  this.execute();
}

Mask.prototype.setFields = function(fd){

   fields=fd;

}

Mask.prototype.setFunction= function(fn){

    func=fn;    

}

Mask.prototype.setEvent= function (ev){

    eventInterator=ev;      

}

Mask.prototype.execute = function(){

    for (var i=0;i<fields.length;i++){
        loadEvent(fields[i]);       
    }

}   

function loadEvent(field){

    $(me+' '+field).on(eventInterator,function() {  

            func();
    });

}

return Mask;

})();

when I run the follow code:

 function doSomeThing(){
   alert("hi");

 }
var fields = ['#field1','#field2','#field3','#field4'];

var mask = new Mask(fields,"keyup",doSomeThing);

I receive the error: Syntax error, unrecognized expression: [object Window]

how can I set the object of javascript class for the jquery selector?

6
  • 1
    what line is the error on? Commented Sep 9, 2015 at 13:17
  • 3
    What happens when you correct this.setFunction(fd); to this.setFields(fd);? Commented Sep 9, 2015 at 13:19
  • 1
    Not related to the question, but I do not see how your Mask abstraction is useful vs $('#field1, #field2, #field3, #field4').keyup(doSomeThing); or $(fields.join(', ')).keyup(doSomeThing) if selectors have to be dynamic. Commented Sep 9, 2015 at 13:29
  • Indeed, by this way the jquery looks like the same thing Commented Sep 9, 2015 at 19:44
  • but I'm curious about using javascript object in jquery Commented Sep 9, 2015 at 19:45

1 Answer 1

3

There are 2 problems

First this.setFunction(fd); should be this.setFields(fd);

Second
In your Mask function, me refers to the window object, since it is an object me + ' ' + field will return [object Window] #field1, that is why you are getting th error.

So change $(me+' '+field) to $(field)


Also as @plalx said, your use of local variables is useless as every instance of Mask will override previous values

Sign up to request clarification or add additional context in comments.

4 Comments

There's more than 2 problems. The entire module doesn't make any sense. All the private module variables will get shared across instances.
The classical implementation is now fine, but honestly that code is useless... jsfiddle.net/2gLf52mm/2
Hi Arun, thanks for your attention. I've tried this alternative, changed $(me+' '+field) to $(field). when I do that, the jquery overlaps all the instances of implemented function to the last function object created. So I want to know if it is possible to add the javascript object as identifier
you are rigth about What change this.setFunction(fd); to this.setFields(fd), it was my mistake, when I passed the code here.

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.