我正在尝试为router5开发一个插件。Javascript Objects交叉引用对方
router5插件是一个函数,它用一个路由器实例并返回一个名称和一些方法的对象。
您可以找到下面的代码的相关部分:
function myPluginFactory(store) {
// So that store can be in the closure
function myPlugin(router){
// The router will reference the store
router.setDependency({'store': store});
// The store will reference the router
store.setRouter(router);
// Public APi
return {
onTransitionStart(fromState, toState){
router.onTransitionStart(fromState, toState);
}
// ...
}
}
myPlugin.pluginName = "MY_PLUGIN";
return myPlugin;
}
class RouterStore {
router = null;
setRouter(router) {
this.router = router;
}
}
// **************** //
import routes from './routes';
import createRouter from 'router5';
const myRouterStore = new RouterStore();
const router = createRouter(routes, {});
// The .usePlugin will call the function returned by
// myPluginFactory(myRouterStore) and call it passing the router instance
router.usePlugin(myPluginFactory(myRouterStore));
所以首先我实例化我的路由器,然后我调用该方法usePlugin创建和我的插件传递到路由器。
我的问题来自myPlugin函数的前2条指令。
那就是:
- 我创建从路由器对象的存储对象
- 参考路由器对象
这将创建一个交叉引用,创建从存储对象的引用。
问:
这是否交叉引用JavaScript创建问题?我可以参加某种通话并打破宇宙吗?
这是一个丑陋的模式,应该避免?