[英]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.