【问题标题】:Javascript, passing a function in an object literal and is it callable?Javascript,在对象文字中传递一个函数,它是可调用的吗?
【发布时间】:2011-10-20 14:15:23
【问题描述】:

一直在学习Javascript和修改一个很酷的autocomplete library,我现在就在前面:

我需要检查在对象字面量中传递的东西是变量/字段(被视为简单值)还是可以调用的东西。

(因为我的自动完成依赖于许多输入字段,我需要在 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 );

【问题讨论】:

    标签: javascript function call


    【解决方案1】:

    你可以做一个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;  
        }
    }
    

    【讨论】:

      【解决方案2】:
         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') {
      
                }
             }
        }
      

      【讨论】:

      • 对我来说很奇怪的是:我如何在 opts 中使用 x 进行迭代,但没有 opts* 有一个名为 *(x) 的属性? (所以我需要用 opts.hasOwnProperty(x) 明确检查它?(缺少')')(+谢谢大家)
      • 您在循环内明确检查它以确保它是一个属性,而不是原型成员。当您开始获取 extraParametersForAjaxRequest 属性时,您不需要明确检查它,因为您可以按名称访问不存在的属性。 (访问不存在的属性会返回 undefined)。两种不同的情况。您之前检查它的方式没有任何问题——这种方式更简洁易读。
      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 2011-06-19
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2018-05-02
      • 2017-08-24
      • 1970-01-01
      相关资源
      最近更新 更多