2015-10-17 66 views
1

我知道我在使用命名空间时做了一些极其错误的事情。在研究净/谷歌搜索中的一个吨后,我发布了这个问题。仍然无法找到我做错了什么。你能帮我解决吗? 这是我Javascript命名空间 - 函数之间的变量交换

的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文件1呼叫js_namespace1.n1function1基于最终用户点击字段

// js_namespace1.n1function1呼叫js_namespace2.n2function1并显示另一个HTML文件2

//在HTML文件2个过程的数据(收集的名称值),然后调用返回功能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中正确使用名称空间 - 推荐的任何最佳实践?

+0

你能提供jsfiddle吗? – Grundy

+3

首先要做的是检查你的开发者控制台。你的代码中有很多错别字('var'而不是'var'等)。 – Pointy

+0

请将您的代码写入编辑器,而不是文字处理器。编程中的拼写和外壳问题。使用'“...”'而不是'“...”,使用'js_namespace ...'而不是'Js_namespace ...'。还有一些缩进将有助于可读性。 – Bergi

回答

0

下面是一个link来自快速Google搜索JS最佳实践。在那里有不同的学校(例如use of terminating semicolons),但是如果你自己不能注意到的话,使用某种类型的linter可能会帮助你找出代码中的拼写错误,区分大小写和空白空白。下面是你的代码的一些修正:

  • 您使用大小写敏感的名字,像js_namespace2Js_namespace2

    (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.n2function1(return_obj) = function(return_obj)

  • 而这里:return_obj. .return_function_to_call_namespace和其他。
  • value_name没有定义

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