(function (window, document, $, undefined) {
"use strict";
//we cache a few useful values, like jQuery wrapped window and document
var $window = $(window),
$document = $(document),
ProjectManagement = {
// Cache Properties
cache: {
jsonPromiseCache: {},
},
init: function(){
console.log('ProjectManagement.init() Function ran');
// Call the User module
ProjectManagement.modules.User.populateUserlistJson('test userlistJsonData data');
},
modules: {
User: {},
Milestones: {},
Tasks: {},
},
};
// Run Init on DOM Ready
$(function() {
ProjectManagement.init();
});
}(this, document, jQuery));
基于上面的确切代码结构。如何将具有自己相似样式功能和属性的对象加载到此属性中:
ProjectManagement.modules.User
这样的东西将被加载到上面我有的代码中ProjectManagement.modules.User
...
(function(){
ProjectManagement.modules.User = {
cache: {
userlistJson: '',
},
init: function(){
console.log('ProjectManagement.modules.User.init() Function ran');
},
populateUserlistJson: function(userlistJsonData){
ProjectManagement.modules.User.cache.userlistJson = userlistJsonData;
console.log('ProjectManagement.modules.User.populateUserlistJson(userlistJsonData) Function ran from ProjectManagement.init()', userlistJsonData);
},
getUsers: function(){
},
};
})();
使用JSFiddle演示进行更新
http://jsfiddle.net/jasondavis/cpvsado1/
在演示中,我收到此错误 Uncaught TypeError: ProjectManagement.modules.User.populateUserlistJson is not a function - line 44
第44行是这样的:ProjectManagement.modules.User.populateUserlistJson('test userlistJsonData data');
它位于ProjectManagement.init()
函数内部。
如果我尝试将代码放在代码ProjectManagement.modules.User
之上,ProjectManagement
那么由于ProjectManagement
尚未定义我的对象,因此我在下面收到此错误:
Uncaught ReferenceError: ProjectManagement is not defined - line 25
第25行是用户对象代码的开头: ProjectManagement.modules.User = {
我正在寻找修改ProjectManagement.modules.User
代码的帮助,以便可以在核心ProjectManagement
对象中访问函数和属性。
我的真实项目有大约6,000行代码,我不想完全更改其核心结构,但是我的“模块”的结构可以根据需要进行更改,以便能够按照我所描述的方式工作。
在我的核心ProjectManagement
对象我希望能够以简单的代码加载到页面的各个模块,并能在我的内这些模块对象的功能和访问性能数据ProjectManagement
obnject
这里的主要问题:ProjectManagement
是一个局部变量,因此不能从另一个函数访问它。
您可以仅将此对象添加到window
对象,并用作全局对象。
您也可以将此对象传递给函数:
(function (window, document, $, undefined) {
"use strict";
//we cache a few useful values, like jQuery wrapped window and document
var $window = $(window),
$document = $(document),
ProjectManagement = {
// Cache Properties
cache: {
jsonPromiseCache: {},
},
init: function(){
console.log('ProjectManagement.init() Function ran');
// Call the User module
ProjectManagement.modules.User.populateUserlistJson('test userlistJsonData data');
},
modules: {
User: {},
Milestones: {},
Tasks: {},
},
};
// Run Init on DOM Ready
$(function() {
ProjectManagement.init();
});
window.ProjectManagement = ProjectManagement;
}(this, document, jQuery));
(function(ProjectManagement){
ProjectManagement.modules.User = {
cache: {
userlistJson: '',
},
init: function(){
console.log('ProjectManagement.modules.User.init() Function ran');
},
populateUserlistJson: function(userlistJsonData){
ProjectManagement.modules.User.cache.userlistJson = userlistJsonData;
console.log('ProjectManagement.modules.User.populateUserlistJson(userlistJsonData) Function ran from ProjectManagement.init()', userlistJsonData);
},
getUsers: function(){
},
};
})(window.ProjectManagement);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句