Javascript Qunit不可见的函数

Javascript Qunit不可见的函数,javascript,function,global-variables,qunit,Javascript,Function,Global Variables,Qunit,大家好,我是JavaScript新手,正在使用Quint框架对我的函数进行单元测试。但我遇到了一个问题,需要专家的帮助 这是我的问题 我有一个名为calc.js的文件,如下所示 var calc; // global variable (function(){ calc = (function(){ function calc(container, options) { this._container = container; th

大家好,我是JavaScript新手,正在使用Quint框架对我的函数进行单元测试。但我遇到了一个问题,需要专家的帮助

这是我的问题

我有一个名为calc.js的文件,如下所示

var calc; // global variable
(function(){
  calc = (function(){
        function calc(container, options) {
           this._container = container;
           this._jq_container = $(container);
           this._options = options || {};
           this.setupDefaults();

        }
        calc.prototype.setupDefaults = function() {
           var _self = this;
          _self._options.type = _self._options.type ? _self._options.type : 'add';
        };
        calc.prototype.add = function (val1, val2) { 
            return val1 + val2;
        };
        calc.prototype.sub = function (val1, val2) { 
            return val1 - val2;
        }; 
  return calc;
  })();
  $.fn.calc = function(options){
    this.each(function(){
      return new calc(this, options);
    });
  };
})();
<!DOCTYPE html>  
<html>  
<head>  
    <title>QUnit Test Suite</title>  
        <link rel="stylesheet" href="qunit/qunit.css">
    <script src="qunit/qunit.js"></script>
        <script src="http://code.jquery.com/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="test/javascript" src="calc.js"></script>   
 <script>
  $(document).ready(function(){

test('add function test', function() { 

 equal(calc.add(2,4),"6" ,"function working correctly");
 /*I have also tried to access add function using some other method but alway got an error*/
});

});
</script>
</head>  
<body>  
    <h1 id="qunit-header">QUnit Test Suite</h1>  
    <h2 id="qunit-banner"></h2>  
    <div id="qunit-testrunner-toolbar"></div>  
    <h2 id="qunit-userAgent"></h2>  
    <ol id="qunit-tests"></ol>  
</body>  
</html> 
现在我有一个qunit的html文件

var calc; // global variable
(function(){
  calc = (function(){
        function calc(container, options) {
           this._container = container;
           this._jq_container = $(container);
           this._options = options || {};
           this.setupDefaults();

        }
        calc.prototype.setupDefaults = function() {
           var _self = this;
          _self._options.type = _self._options.type ? _self._options.type : 'add';
        };
        calc.prototype.add = function (val1, val2) { 
            return val1 + val2;
        };
        calc.prototype.sub = function (val1, val2) { 
            return val1 - val2;
        }; 
  return calc;
  })();
  $.fn.calc = function(options){
    this.each(function(){
      return new calc(this, options);
    });
  };
})();
<!DOCTYPE html>  
<html>  
<head>  
    <title>QUnit Test Suite</title>  
        <link rel="stylesheet" href="qunit/qunit.css">
    <script src="qunit/qunit.js"></script>
        <script src="http://code.jquery.com/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="test/javascript" src="calc.js"></script>   
 <script>
  $(document).ready(function(){

test('add function test', function() { 

 equal(calc.add(2,4),"6" ,"function working correctly");
 /*I have also tried to access add function using some other method but alway got an error*/
});

});
</script>
</head>  
<body>  
    <h1 id="qunit-header">QUnit Test Suite</h1>  
    <h2 id="qunit-banner"></h2>  
    <div id="qunit-testrunner-toolbar"></div>  
    <h2 id="qunit-userAgent"></h2>  
    <ol id="qunit-tests"></ol>  
</body>  
</html> 

QUnit测试套件
$(文档).ready(函数(){
test('addfunctiontest',function(){
相等(计算添加(2,4),“6”,“功能正常工作”);
/*我还尝试使用其他方法访问add函数,但总是出现错误*/
});
});
QUnit测试套件
我的html和calc.js都在同一个文件中 当我运行这个文件时,我得到一个错误,calc没有定义

我不明白为什么,因为我已经使calc全球化了。 我也尝试过使用
window.calc=calc
,但都是徒劳的 有人能告诉我如何在html测试文件中访问这些函数吗
任何帮助都将不胜感激

我在jsfiddle.net中运行了您的代码块,并调用了
calc.add(2,4)未运行


我将调用更改为
calc.prototype.add(2,4)
,我可以按预期调用您的函数。我建议您将
.prototype
添加到QUnit测试中?

我在jsfiddle.net中运行了您的代码块,并调用了
calc.add(2,4)未运行


我将调用更改为
calc.prototype.add(2,4)
,我可以按预期调用您的函数。我建议您将
.prototype
添加到QUnit测试中?

忽略此测试的QUnit方面。。甚至可以从脚本块调用calc函数吗?忽略此函数的QUnit方面。。甚至可以从脚本块调用calc函数吗?