基本的JavaScript对象结构有助于访问属性

杰森·戴维斯
(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对象我希望能够以简单的代码加载到页面的各个模块,并能在我的内这些模块对象的功能和访问性能数据ProjectManagementobnject

格伦迪

这里的主要问题:ProjectManagement是一个局部变量,因此不能从另一个函数访问它。

您可以仅将此对象添加到window对象,并用作全局对象。

JSFiddle

您也可以将此对象传递给函数:

(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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

TSQL查询有助于结构化结果

来自分类Dev

TSQL查询有助于结构化结果

来自分类Dev

哪些属性有助于LCD面板的质量?

来自分类Dev

有助于Google翻译的API

来自分类常见问题

索引是否有助于sql选择排序性能?

来自分类Dev

线程有助于提高Java的效率吗?

来自分类Dev

流浪汉如何有助于发展

来自分类Dev

与流有助于消除从文件扩展名

来自分类Dev

索引是否有助于sql选择排序性能?

来自分类Dev

合并有助于SQL中聚合的值

来自分类Dev

数组as3冲突有助于编码

来自分类Dev

Eclipse内容有助于Gradle依赖性

来自分类Dev

Eclipse RCP:这是否有助于片段项目

来自分类Dev

Redis复制是否有助于负载均衡?

来自分类Dev

线程化有助于提高Java的效率吗?

来自分类Dev

哪些方法有助于改善无线信号?

来自分类Dev

硬件是否有助于减少OS X的崩溃?

来自分类Dev

AsyncController方法声明,有助于理解语法

来自分类Dev

if语句有助于控制无人机

来自分类Dev

启用引导内容有助于日食

来自分类Dev

TLP是否真的有助于延长电池寿命?

来自分类Dev

制作有助于故障诊断的功能

来自分类Dev

xtext,内容有助于不必要的建议

来自分类Dev

嵌套循环是否有助于解析结果

来自分类Dev

在C ++中设计一个小的对象池,这有助于减少“对对象的重复操作”

来自分类Dev

使用 aria-hidden="true" 隐藏 Flash 元素和 iFrame 是否有助于使我的网站可访问?

来自分类Dev

与CISC相比,为什么RISC体系结构的较小指令集并不一定有助于减少CPU时间?

来自分类Dev

是否有任何数学算法有助于良好的响应式html设计?

来自分类Dev

使用CSRF表单令牌是否有助于预防垃圾邮件?

Related 相关文章

热门标签

归档