简体   繁体   English

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

[英]calling javascript namespace function from within jquery

I want to populate a jquery ui modal dialog with values from an array in this 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"],
      ...          ];         
    return {
        values: function(index1, index2) {
        return arr[index1][index2];
        }
    };
}(); 

this works OK when called like this: 当这样调用时,它可以正常工作:

console.log(WsdArrays.values(0, 0));

but it does not work when the module function is called within a jquery statement with arguments generated by the jquery click event thus: 但是,当在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);
});

What needs to be changed here? 这里需要更改什么?

Use window to make it more global. 使用window使其更具全局性。

So set it like so: 因此,将其设置为:

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];
        }
    };
}(); 

This way it will sit in the global namespace, if it is made within another function this will also work. 这样,它将位于全局名称空间中,如果它是在另一个函数中创建的,则它也将起作用。 If you declare it with var inside another function it will only be available within that function. 如果在另一个函数中用var声明它,则仅在该函数中可用。

I think scoping is here indeed the main issue. 我认为scoping确实是主要问题。 But instead of littering the window /global namespace I suggest using another namespace: 但是建议不要使用window / 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);

So, there is no need for using global scope, and WsdArrays is visible to the onClick . 因此,无需使用全局范围,并且onClick可以看到WsdArrays。

Besides: why aren't you using a dictionary-approach? 另外:为什么不使用字典方法?

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

Another thing: Why are you encapsulating a simple function in an IFFE? 另一件事:为什么将简单功能封装在IFFE中? Only for namespacing reasons? 仅出于命名空间的原因? A simple function would be sufficient. 一个简单的功能就足够了。

Try parseInt: 尝试parseInt:

index1 = parseInt(elementId.substr(7)),

The reason is your index1 without parseInt is a string "01" after calling elementId.substr(7) for "wsd_img01" . 原因是在为"wsd_img01"调用elementId.substr(7)之后,没有parseIntindex1是字符串"01" "wsd_img01" This will cause problem as arr["01"] returns undefined which causes exception when you call arr["01"][index2] . 这将导致问题,因为当您调用arr["01"][index2]arr["01"]返回undefined会导致异常。 As you can see in the following DEMO 如下面的演示所示

When we use parseInt , index1 becomes 1 which does not cause problem. 当我们使用parseIntindex1变为1,这不会引起问题。

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM