Javascript,在对象文字中传递函数并且它可以调用吗?

发布于 2024-12-11 01:32:01 字数 1605 浏览 0 评论 0原文

总是在学习Javascript和修改一个很酷的自动完成库的过程中,我现在在此之前:

我需要检查对象文字中传递的内容是否是变量/字段(即被视为简单值)或者是可以调用的内容。

(因为我的自动完成取决于许多输入字段,所以我需要在 Ajax.Request 之前“评估”正确的内容)以便此声明(请参阅“额外”部分...)

   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; } 
    }

看到“1 // 这里我'我迷路了......”下面,而不是 1 =>我想检查 extraParametersForAjaxRequest[x] 是否可调用,如果是,则调用它,如果不是,则仅保留其值。这样,我就得到了其他输入的正确值......同时保持真正通用的方法和对该库的干净修改......

{
  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 );

always in the process of learning Javascript and modifying a cool autocomplete library, i am now in front of this :

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.

(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...)

   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; } 
    }

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...

{
  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 );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-12-18 01:32:01

您可以执行 typeof 来查看参数是否是函数,如果是则调用它。

var value;
for (var x in this.options.extraParametersForAjaxRequest)
{
    value = this.options.extraParametersForAjaxRequest[x];

    if (typeof(value) == 'function') {
        ajaxOptions.parameters[x] = value();
    }
    else {
        ajaxOptions.parameters[x] = value;  
    }
}

You can do a typeof to see if the parameter is a function, and call it if it is.

var value;
for (var x in this.options.extraParametersForAjaxRequest)
{
    value = this.options.extraParametersForAjaxRequest[x];

    if (typeof(value) == 'function') {
        ajaxOptions.parameters[x] = value();
    }
    else {
        ajaxOptions.parameters[x] = value;  
    }
}
樱花落人离去 2024-12-18 01:32:01
   if (typeof this.options.extraParametersForAjaxRequest[x]==='function') {

   }

您还应该这样做:

   if (this.options.extraParametersForAjaxRequest.hasOwnProperty(x) {
       if (typeof this.options.extraParametersForAjaxRequest[x]==='function') {

       }
   }

在迭代对象的属性时,否则您最终也可能会查看原型成员。

另一个建议是使用您正在使用的东西的别名来使其更具可读性。所以最终的结果是:

  var opts = this.options.extraParametersForAjaxRequest;
  // don't need to check for existence of property explicitly with hasOwnProperty
  // just try to access it, and check to see if the result is
  // truthy. if extraParametersForAjaxRequest isn't there, no error will
  //  result and "opts" will just be undefined
  if (opts)
  {
      for (var x in opts) {
          if (opts.hasOwnProperty(x) && typeof opts[x]==='function') {

          }
       }
  }
   if (typeof this.options.extraParametersForAjaxRequest[x]==='function') {

   }

You should also do this:

   if (this.options.extraParametersForAjaxRequest.hasOwnProperty(x) {
       if (typeof this.options.extraParametersForAjaxRequest[x]==='function') {

       }
   }

when iterating through properties of objects, otherwise you can end up looking at prototype members too.

Another suggestion is to make this more readable with an alias for the thing you're working with. So the ultimate would be:

  var opts = this.options.extraParametersForAjaxRequest;
  // don't need to check for existence of property explicitly with hasOwnProperty
  // just try to access it, and check to see if the result is
  // truthy. if extraParametersForAjaxRequest isn't there, no error will
  //  result and "opts" will just be undefined
  if (opts)
  {
      for (var x in opts) {
          if (opts.hasOwnProperty(x) && typeof opts[x]==='function') {

          }
       }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文