javascript / jquery:在对象内调用函数的问题(函数未定义)

时间:2011-09-05 15:42:19

标签: javascript jquery

运行此代码时出现“buildXML未定义”的错误:

var c = {

  updateConsumer:function (cid,aid,sid,survey){
    var surveyXML = buildSurveyXML(survey);
  },

  buildSurveyXML: function(survey) { 
    var surveyResults = survey.split("|");
    var surveyXML = '';

    for (var i=0;i<surveyResults.length;i++){
       ...
    }

    return surveyXML;
  }
}

包含此JS的html并调用updateConsumer函数:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <title>Web Service Test</title>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="../../shared/js/consumerSoap.js"></script>
    </head>

    <body>
        <script type="text/javascript">
             $(document).ready(function() {
              c.insertConsumer("First","Last","55555","name@url.com","76:1139");
             });
        </script>
    </body>
</html>

2 个答案:

答案 0 :(得分:2)

问题是updateConsumerbuildSurveyXML一无所知;该功能不在全球范围内。但是,由于您的函数是同一对象的一部分,因此您可以使用this关键字来调用它。

updateConsumer:function (cid,aid,sid,survey){
    var surveyXML = this.buildSurveyXML(survey);
}

答案 1 :(得分:0)

使用 var surveyXML = c.buildSurveyXML(survey);