【问题标题】:Javascript namespace - exchange of variable between functions [duplicate]Javascript命名空间 - 函数之间的变量交换[重复]
【发布时间】:2016-01-16 05:32:51
【问题描述】:

我知道我在使用命名空间时做错了什么。在网络/谷歌搜索中研究了大量内容后,我发布了这个问题。仍然找不到我做错了什么。你能帮帮我吗? 这就是我所拥有的

Javascript

Javascript 文件1

(function (js_namspace1, $, undefined) {
    js_namespace1.n1function1 = function(){

      var return_obj = {
         return_function_to_call: “n1function_name2”
         return_function_to_call_namespace: “js_namespace1”
       }
      js_namespace2.n2function1(return_obj)
    }
    Js_namespace1.n1function_name2 =function(list_of_names){
        Js_namespace1.list_of_names = list_of_names
        // do some processing to js_namespace1. list_of_names
    }
}
(window. js_namspace1 = window. js_namspace1|| {}, jQuery ));

Javascript 文件2

(function (js_namspace2, $, undefined) {
    js_namespace2.n2function1(return_obj) = function(return_obj){
    js_namespace2.return_function_to_call =     return_obj.return_function_to_call
    js_namespace2.return_function_to_call_namespace = return_obj.  .return_function_to_call_namespace

    // do some processing
    Js_namespace2.list_of_names = []
    Js_namespace2. list_of_names.push(value_name)
    window[js_namespace2.return_function_to_call_namespace][js_namespace2.return_function_to_call]( Js_namespace2.list_of_names);
  }
}
(window. js_namspace2 = window. js_namspace2|| {}, jQuery ));

HTML

从 html file1 调用 js_namespace1.n1function1 基于最终用户单击字段

//js_namespace1.n1function1调用js_namespace2.n2function1并显示另一个html文件2

//在html file2中处理数据(收集name的值)然后调用返回函数Js_namespace1.n1function_name2

Js_namespace1.n1function_name2,处理Js_namespace1.list_of_names(array),但是当我这样做时,它也在Js_namespace2.list_of_names中改变

例如,当我这样做时 Js_namespace1.n1function_name2.push(add_another_name),然后调用js_namespace1.n1function1(它又调用js_namespace2.n2function1)。 Js_namespace2.list_of_names 包含 add_another_name 的值。

请注意,当从js_namespace1.n1function1 调用js_namespace2.n2function1 时,数组不会作为参数传递。

我的期望是当js_namespace1.n1function1 调用js_namespace2.n2function1不会更新 Js_namespace2.list_of_namesadd_another_name

你能解释一下发生了什么吗?最重要的是指出我在这个设计中应该避免的任何错误(命名空间、函数调用之间的参数交换)。我是否在 javascript 中正确使用了命名空间——有什么可推荐的最佳实践吗?

【问题讨论】:

  • 可以提供jsfiddle吗?
  • 首先要做的是检查您的开发者控制台。您的代码中有很多拼写错误(Var 而不是 var 等)。
  • 请在编辑器中编写您的代码,而不是文字处理器。拼写和大小写在编程中很重要。使用"…" 而不是“…”,使用js_namespace… 而不是Js_namespace…。一些缩进也会对可读性有很大帮助。
  • 另请参阅Copying array by value in JavaScript 以获得您的问题的解决方案。

标签: javascript javascript-namespaces


【解决方案1】:

这里有一个link,来自一个关于 JS 最佳实践的快速 Google 搜索。那里有不同的思想流派(例如use of terminating semicolons),但是如果您自己没有注意到,使用某种 linter 可能会帮助您找出代码中的拼写错误、区分大小写和意外空白。以下是您的代码并进行了一些修复:

(function (js_namespace1, $, undefined) {

    js_namespace1.n1function1 = function(){

      var return_obj = {
         return_function_to_call: "n1function_name2",
         return_function_to_call_namespace: "js_namespace1"
       };
      js_namespace2.n2function1(return_obj)
    };
    js_namespace1.n1function_name2 =function(list_of_names){
        js_namespace1.list_of_names = list_of_names;
        console.log(js_namespace1.list_of_names); // ["some_name"]
    };
}
(js_namespace1 = window.js_namespace1 || {}, jQuery));

(function (js_namespace2, $, undefined) {
    js_namespace2.n2function1 = function(return_obj){
    js_namespace2.return_function_to_call =  return_obj.return_function_to_call;
    js_namespace2.return_function_to_call_namespace = return_obj.return_function_to_call_namespace;

    // do some processing
    js_namespace2.list_of_names = [];
    js_namespace2.list_of_names.push("some_name");
    window[js_namespace2.return_function_to_call_namespace][js_namespace2.return_function_to_call]( js_namespace2.list_of_names);
  };
}
(js_namespace2 = window.js_namespace2 || {}, jQuery));
js_namespace1.n1function1();

关于你的代码和我的修复的几点:

  • 您使用了区分大小写的名称,例如 js_namespace2 用于 Js_namespace2
  • 您在此处的语法不正确js_namespace2.n2function1(return_obj) = function(return_obj)
  • 在这里:return_obj. .return_function_to_call_namespace 和其他人。
  • value_name 未定义

我测试了代码 here 并看到了预期的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多