JavaScript Closure Problem
Posted: Fri Nov 15, 2019 9:44 pm
Code: Select all
function print (msg) {
if (typeof(msg) == "undefined"){
// getting an undefined causes error as interpreter does not
// know if string or object
Packages.java.lang.System.out.println("undefined");
return;
}
// next line for debugging
//Packages.java.lang.System.out.println("[print 59] typeof msg = " + typeof(msg));
if (typeof(msg) == "object"){
Packages.java.lang.System.out.println("[toString] " + msg.toString);
} else {
Packages.java.lang.System.out.println(msg);
}
}
function applicationStarted(pluginWorkspaceAccess){
myWorkspaceAccess = pluginWorkspaceAccess;
var edChangedListener = {
var editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, Packages.ro.sync.exml.workspace.api.PluginWorkspace.MAIN_EDITING_AREA);
if(editor.getCurrentPageID() == "Author"){
actionApple = {
customizePopUpMenu: function (popUp, authorAccess) {
try {
documentController = authorAccess.getDocumentController();
mi = new Packages.javax.swing.JMenuItem("Print Apples");
popUp.add(mi);
actionPerfObj = {
actionPerformed: function (e) {
print("Apples @ 7th level");
// want to call methods on authorAccess and documentController, e.g. process nodes with Apples in them
} // 7 levels of nesting! whew!
}
}
}
}
actionBanana = {
customizePopUpMenu: function (popUp, authorAccess) {
try {
documentController = authorAccess.getDocumentController();
mi = new Packages.javax.swing.JMenuItem("Print Bananas");
popUp.add(mi);
actionPerfObj = {
actionPerformed: function (e) {
print("Bananas @ 7th level");
// want to call methods on authorAccess and documentController, e.g. process nodes with Bananas in them
}
}
}
}
}
actionCherries = {
customizePopUpMenu: function (popUp, authorAccess) {
try {
documentController = authorAccess.getDocumentController();
mi = new Packages.javax.swing.JMenuItem("Print Cherries");
popUp.add(mi);
actionPerfObj = {
actionPerformed: function (e) {
print("Cherries @ 7th level");
// want to call methods on authorAccess and documentController, e.g. process nodes with Cherries in them
}
}
}
}
}
actionDoAll = {
customizePopUpMenu: function (popUp, authorAccess) {
try {
documentController = authorAccess.getDocumentController();
mi = new Packages.javax.swing.JMenuItem("Do All");
popUp.add(mi);
actionPerfObj = {
actionPerformed: function (e) {
// want to do each of the three calls of methods for Apples, Bananas & Cherries above
}
}
}
}
}
}
}
}
I think this is a closure issue. I tried to isolate the code that would be at the 7th level into separate functions at the global level like my "print" function so they could be called from more than one place, but at load time, I get error messages about undefined values. This would coincide with my understanding of closure that the code is executed when called upon when various senior variables are defined, not at load time.
How can I structure code that would be executed say at the Apples 7th level and then executed at the DoAll 7th level without acutally having the code written out in each one? I'd like to have a single source of truth.