How to pass parameters in function object literal


CalAlt

I want to pass integers as min and max parameters to a function to return a random number for use by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not a function". How can I return randomGeneratorthe numbers used from it interaction?

bindEvents: function() {
    $('#left').on("click", this.interaction);
},
interaction: function() {
   var selector = this.randomGenerator(0, 3);
   var $columns = $('#left').find('div[col]');

   $columns.children().removeClass('show');
   $columns.eq(selector).children().addClass('show');
},
randomGenerator: function(min, max) {
    var last,      
        value,     
        count = 0, 
        getR = function () { return Math.floor(Math.random() * (max - min)) + min; };

    return function () {
        var random;
        if (count && value !== last) {
           --count;
            return last = value;
        }
        random = getR();
        while (random === last) {
             value = random;
             ++count;
            random = getR();
        }
        return last = random;
    };
},

Rory McCrossan

The problem is because thisin the clickhandler you are referencing the clicked element, not the object containing the randomGenerator()function .

To work around this, you can keep a reference to it in a variable before thisusing $.proxy()(or bind()) setting it to the scope randomGenerator()under which the function should run . Try this:

var obj = {
  bindEvents: function() {
    var _this = this;
    $('#left').on("click", $.proxy(_this.interaction, _this));

    // Note you can also use the native bind() method, if preferred:
    // $('#left').on("click", _this.interaction.bind(_this));
  },
  interaction: function() {
    var selector = this.randomGenerator(0, 3);
    console.log(selector()); // just for testing...
    
    var $columns = $('#left').find('div[col]');

    $columns.children().removeClass('show');
    $columns.eq(selector).children().addClass('show');
  },
  randomGenerator: function(min, max) {
    var last,
      value,
      count = 0,
      getR = function() {
        return Math.floor(Math.random() * (max - min)) + min;
      };

    return function() {
      var random;
      if (count && value !== last) {
        --count;
        return last = value;
      }
      random = getR();
      while (random === last) {
        value = random;
        ++count;
        random = getR();
      }
      return last = random;
    };
  }
}

obj.bindEvents();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="left">Click me</div>

Related


How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass parameters in function object literal

CalAlt I would like to pass integers as min and max parameters to a function to return a random number to be used by another function. I am new to the Java language and am using the object literal pattern. Currently I get the error "this.randomGenerator is not

How to pass object properties as function parameters?

Gross weight I am designing a three-tier framework I'm wondering if it's possible to pass properties of an object to a function without explicitly declaring them? For example, if I want to pass Id,NametopersonnelBL.ValidateInsert(...) I don't want the Validate

How to pass object properties as function parameters?

Gross weight I am designing a three-tier framework I'm wondering if it's possible to pass properties of an object to a function without explicitly declaring them? For example, if I want to pass Id,NametopersonnelBL.ValidateInsert(...) I don't want the Validate

How to pass a function to an array literal?

other How can I pass an array without making it a separate variable? For example, I know this works: class Test{ public static void main(String[] args){ String[] arbitraryStrings={"foo"}; takesStringArray(arbitraryStrings); } public

How to pass parameters in a function

username How can I pass the value of the textbox, this works for the first select box, why doesn't it work for all boxes. If I check this checkbox, the textbox value is set to null <input type="checkbox" name="onoffswitch'.$i.'" class="onoffswitch'.$i.'-check

How to pass parameters in a function

username How can I pass the value of the textbox, this works for the first select box, why doesn't it work for all boxes. If I check this checkbox, the textbox value is set to null <input type="checkbox" name="onoffswitch'.$i.'" class="onoffswitch'.$i.'-check

How to pass a function with parameters to a function?

High purity base I want to do something like this int x = 2; int y = 3; int performAction(int var1, int var2) { return var1 + var2; } //accepts function with parameters as parameter void runLater(performAction(x, y)); The values of x and y may change be

How to pass object literal as polymer property

y blood To test some of my polymer custom elements in isolation, I'd like to be able to pass js object literals that usually come from some properties of the parent element. I'm having trouble figuring out how to do this. See this sample code. If it works as I

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass string literal to filter function in JavaScript

random IT I want to pass string literal to filter function. The result should be filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 && o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1 What I am currently

How to pass parameters as a single object?

Adnand I have the following example to show you what I am looking for: private void myMethod(string a = "", string b = "", string c = "") { // do things } I want to find a way to call the method like this: MyParameterObject parameters = new MyParameterObje

How to pass parameters to object query?

Winnie Varez Sosotona I have an object query (getUserLogin) in MS Access which will do the following: PARAMETERS prmUsername Text, prmPassword Text; SELECT ID, LastName, FirstName, MiddleName FROM tblUsers WHERE Username = [prmUsername] AND Password = [prmPas

How to pass object properties as parameters?

that one For example, I have this function using node classification: function example(){ var exampleVar = nodes.classfication; //below i use exampleVar to do calculations . . . . } But say sometimes I want to get node state, so I do: function example(){

How to pass parameters as a single object?

Adnand I have the following example to show you what I am looking for: private void myMethod(string a = "", string b = "", string c = "") { // do things } I want to find a way to call the method like this: MyParameterObject parameters = new MyParameterObje

How to pass parameters as a single object?

Adnand I have the following example to show you what I am looking for: private void myMethod(string a = "", string b = "", string c = "") { // do things } I want to find a way to call the method like this: MyParameterObject parameters = new MyParameterObje

How to pass object properties as parameters?

that one For example, I have this function using node classification: function example(){ var exampleVar = nodes.classfication; //below i use exampleVar to do calculations . . . . } But say sometimes I want to get node state, so I do: function example(){

How to pass object properties as parameters?

that one For example say I have this function using node classification: function example(){ var exampleVar = nodes.classfication; //below i use exampleVar to do calculations . . . . } But say sometimes I want to get node state, so I do: function example(){

How to pass parameters as a single object?

Adnand I have the following example to show you what I am looking for: private void myMethod(string a = "", string b = "", string c = "") { // do things } I want to find a way to call the method like this: MyParameterObject parameters = new MyParameterObje

How to pass parameters as a single object?

Adnand I have the following example to show you what I am looking for: private void myMethod(string a = "", string b = "", string c = "") { // do things } I want to find a way to call the method like this: MyParameterObject parameters = new MyParameterObje