我知道我在使用命名空间时做了一些严重错误的事情。我在网络/谷歌搜索中进行了大量研究后发布了这个问题。仍然找不到我做错了什么。你能帮我一下吗? 这就是我所拥有的
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中处理数据(收集名字的值)然后调用返回函数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_namespace2.n2function1
时从 js_namespace1.n1function1
调用数组不作为参数传递。
我的期望是 js_namespace1.n1function1
来电 js_namespace2.n2function1
它不会更新 Js_namespace2.list_of_names
与 add_another_name
。
您能解释一下发生了什么吗?最重要的是指出我在这个设计中应该避免的任何错误(命名空间、函数调用之间的参数交换)。我是否在 javascript 中正确使用了命名空间 – 有什么最佳实践值得推荐吗?
最佳答案
这是一个link通过 Google 快速搜索 JS 最佳实践。有不同的思想流派(例如 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并查看预期的行为。
关于Javascript 命名空间 - 函数之间的变量交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187836/