简体   繁体   English

如何从另一个模块JS对象中的JavaScript对象访问函数和变量?

[英]How can I access functions and variables from my JavaScript object inside of another module JS object?

I am building an app with JavaScript and PHP on the backend. 我正在后端使用JavaScript和PHP构建应用程序。 It is going to allow users to build plugins/modules to extend the app. 它将允许用户构建用于扩展应用程序的插件/模块。

Based on a JavaScript structure like the object below var WebDevApp = {} 基于JavaScript结构,如var WebDevApp = {}下的对象

How could I make other Modules which inclkude there own JavaScript file into the page, be able to access there object in my core code below and also allow them to access this object in there code. 我如何才能使包含其中自己的JavaScript文件的其他模块进入页面,如何能够在下面的我的核心代码中访问那里的对象,并允许他们在那里的代码中访问该对象。

For example if a module named Bookmarks was loaded it would have a JS object like this... 例如,如果加载了一个名为Bookmarks的模块,则它将具有这样的JS对象...

var WebDevApp.Bookmarks = {

}  

And in my core code below I could call functions from above like this... 在下面的核心代码中,我可以像这样从上方调用函数...

WebDevApp.Bookmarks.getJsonData();

Inside of a module JS code I should be able to call something from the core code like this... 在模块JS代码内部,我应该能够从核心代码中调用诸如此类的东西...

WebDevApp.cache.varFromCoreJsObjectCalledFromModule

My core JS object code which I want modules to extend from... 我想要模块扩展的核心JS目标代码...

var WebDevApp = {

    modulesJson: '',

    cache: {
        templates: {},
    },

    init: function() {

    },

    events: function() {

        // all the dom devents from other JS files ................
        WebDevApp.Bookmarks.domEvents();

        WebDevApp.OtherModuleName.domEvents();
    },


    loadJSONData: function() {
        // load JSON Data from the other Module JS files
        WebDevApp.Bookmarks.loadJSONData();

        WebDevApp.OtherModuleName.loadJSONData();
    },
}


// run app
jQuery(document).ready(function($) {
    WebDevApp.init();
});

I think you'd probably want to make sure that your core module is not tightly coupled to the other modules. 我认为您可能要确保您的核心模块没有与其他模块紧密耦合。 You could do that by making sure that you register the other modules with the core (assumes a modules Array property exists): 您可以通过确保向核心注册其他模块来做到这一点(假设存在一个模块Array属性):

WebDevApp.modules.push(WebDevApp.Bookmarks);

Then in the events and loadJSONData functions, you'd have logic like this: 然后在事件和loadJSONData函数中,您将具有如下逻辑:

WebDevApp.modules.forEach(function(mod){
    mod.domEvents();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个Javascript文件中使用游戏对象及其功能/变量 - How can I use my game object and its functions/variables in another Javascript File 函数中的Javascript访问对象变量 - Javascript access object variables from functions JavaScript:如何访问另一个对象内部的对象原型? - JavaScript: How do I access the prototype of an object inside another object? 如何访问JavaScript中另一个object的数据? - How can I access a data from another object in JavaScript? 如何访问另一个对象内的javascript对象 - How to access javascript object inside another object 如何从单个内部的另一个对象访问单个“私有函数” - How to access 'private functions' in a singleton from another object inside it 如何在Javascript中的另一个对象中添加对象? - How can I add an object inside another object in Javascript? JS:如何访问另一个函数内部的函数中的变量值,并且两个函数都属于同一对象? - JS: How may I access a variable value inside a function that's inside another function, and that both functions belong to the same object? 如何使用node.js从一个模块到另一个模块访问JSON对象? - How do I access a JSON object from one module to another using node.js? 如何从 JavaScript 中的对象内部访问属于同一对象的属性? - How can I access properties that belong to the same object from inside the object in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM