Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript, passing a function in an object literal and is it callable?
    text
    copied!<p>always in the process of learning Javascript and modifying a cool <a href="http://www.devbridge.com/projects/autocomplete/#download" rel="nofollow noreferrer">autocomplete library</a>, i am now in front of this :</p> <p>i need to check if something passed in an object literal is a variable/field (that is to be considered as a simple value) or is something that can be called.</p> <p>(as MY autocomplete depend on many input fields, i need to "value" the right things, just before the Ajax.Request) so that this declaration (see the 'extra' parts...)</p> <pre><code> myAutoComplete = new Autocomplete('query', { serviceUrl:'autoComplete.rails', minChars:3, maxHeight:400, width:300, deferRequestBy:100, // callback function: onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); } // the lines below are the extra part that i add to the library // an optional parameter, that will handle others arguments to pass // if needed, these must be value-ed just before the Ajax Request... , extraParametersForAjaxRequest : { myExtraID : function() { return document.getElementById('myExtraID').value; } } </code></pre> <p>see the "1 // here i'm lost..." below, and instead of 1 => i would like to check, if extraParametersForAjaxRequest[x] is callable or not, and call it if so, keeping only its value if not. So that, i get the right value of my other inputs... while keeping a really generic approach and clean modification of this library...</p> <pre><code>{ var ajaxOptions = { parameters: { query: this.currentValue , }, onComplete: this.processResponse.bind(this), method: 'get' }; if (this.options.hasOwnProperty('extraParametersForAjaxRequest')) { for (var x in this.options.extraParametersForAjaxRequest) { ajaxOptions.parameters[x] = 1 // here i'm lost... } } new Ajax.Request(this.serviceUrl, ajaxOptions ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload