从jquery中调用javascript命名空间函数

从jquery中调用javascript命名空间函数,javascript,jquery,module,Javascript,Jquery,Module,我想使用此模块中数组的值填充jquery ui模式对话框: var WsdArrays = function() { var arr = [["the boy is going home", "wsd_aud1"], ["You and I are friends", "wsd_aud2"], ["He has a book of mine", "wsd_aud3"], ["He will run to his home", "wsd_aud4"],

我想使用此模块中数组的值填充jquery ui模式对话框:

var WsdArrays = function() {
     var arr = [["the boy is going home", "wsd_aud1"],
      ["You and I are friends", "wsd_aud2"],
      ["He has a book of mine", "wsd_aud3"],
      ["He will run to his home", "wsd_aud4"],
      ...          ];         
    return {
        values: function(index1, index2) {
        return arr[index1][index2];
        }
    };
}(); 
当这样调用时,它工作正常:

console.log(WsdArrays.values(0, 0));
但是,在jquery语句中调用模块函数时,如果jquery click事件生成参数,则该函数不起作用,因此:

jQuery("span.ws_dialog_icon").on("click",function(evnt) {
  jQuery("div#ws_dialog").dialog("open");
  evnt.stopPropagation();
  var elementId = evnt.target.id,
      index1 = elementId.substr(7), //strips chars from ids like "wsd_img01"
      index2 = 0,
      arrayVals = WsdArrays.values(index1, index2);
  jQuery("div#wsd_text").text(arrayVals);
});

这里需要改变什么

使用
窗口
使其更具全局性

这样设置:

window.WsdArrays = function() {
     var arr = [["the boy is going home", "wsd_aud1"],
      ["You and I are friends", "wsd_aud2"],
      ["He has a book of mine", "wsd_aud3"],
      ["He will run to his home", "wsd_aud4"],
      ...          ];         
    return {
        values: function(index1, index2) {
        return arr[index1][index2];
        }
    };
}(); 

这样,它将位于全局名称空间中,如果它是在另一个函数中创建的,那么它也可以工作。如果您在另一个函数中使用
var
声明它,它将仅在该函数中可用。

我认为
范围界定确实是主要问题。
但是我建议使用另一个名称空间,而不是乱扔
窗口
/global名称空间:

var app=function($){
    var WsdArrays = function() {
        var arr = [["the boy is going home", "wsd_aud1"],
        ["You and I are friends", "wsd_aud2"],
        ["He has a book of mine", "wsd_aud3"],
        ["He will run to his home", "wsd_aud4"],
        ...          ];         
        return {
                 values: function(index1, index2) {
                         return arr[index1][index2];
               }
        };
    }();
    $("span.ws_dialog_icon").on("click",function(evnt) { 
       $("div#ws_dialog").dialog("open");
       evnt.stopPropagation();
       var elementId = evnt.target.id,
       index1 = elementId.substr(7), //strips chars from ids like "wsd_img01"
       index2 = 0,
       arrayVals = WsdArrays.values(index1, index2);
       $("div#wsd_text").text(arrayVals);
   });
}(jQuery);
因此,不需要使用全局范围,wsdarray对
onClick
可见

此外:你为什么不使用字典法

var WsdArrays=function(){
    var whatEverString={
         "wsd_aud1","the boy is going home",
         "wsd:auds","you and i are friends"
    };
    return {
        values:function(id){ return whatEverString[id]; }
    }
}
还有一件事: 为什么要在IFFE中封装一个简单的函数?只是因为名称空间的原因? 一个简单的函数就足够了。

试试parseInt:

index1 = parseInt(elementId.substr(7)),
原因是您的
index1
没有
parseInt
是在调用
elementId.substr(7)
后为
“wsd\u img01”
的字符串。这将导致问题,因为
arr[“01”]
返回
undefined
,这将在调用
arr[“01”][index2]
时导致异常。正如你在下面看到的

当我们使用
parseInt
时,
index1
变为1,这不会引起问题


但在jquery语句中调用模块函数时,它不起作用
它是否导致异常或只是返回错误的结果?控制台说什么?您传递的值是什么?您是否尝试过在
函数中添加
控制台(index1,index2)
?另外,
wsdarray
在哪里定义?你说
namespaced函数
名称空间在哪里?它与jQuery代码在同一个闭包中、在同一个文件中还是完全分开?代码不起作用(在删除…)没有显示对话框,而在显示对话框之前是空的。我使用数组是因为我认为它检索值的速度比文本对象快@Thomas Junk您是否可以创建一个对话框,在这里我们可以看到更多的代码和不起作用的内容。对话框显示但未填充。JSHint说WsdArrays是未定义的??我最初收到的信息是:(@Jordan Ramstad